Handling dates that can be successfully parsed by new Date(date_string)

Tell us what’s happening:

I finished this project with the full functionality as the challenge asks but the test says

Your project can handle dates that can be successfully parsed by new Date(date_string)

and I don’t understand what this mean ?

Your project link(s)

solution: https://replit.com/@smailove/boilerplate-project-timestamp-2

Your browser information:

User Agent is: Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/92.0.4515.107 Safari/537.36

Challenge: Timestamp Microservice

Link to the challenge:

More specifically:

  1. no argument (new Date()) will give an object representing the datetime at the point in time the function was called
  2. an integer representing the number of milliseconds elapsed since 1st January 1970 at 00:00:00 (this will be 13 digits) will give a date object representing that datetime
  3. a string in one of the following ISO-compliant forms – Date.parse() - JavaScript | MDN – will give a date object representing that datetime

So it’s number 3 that you need to look at

1 Like

I didn’t understand your replay (the 3 points you mentioned) can you elaborate please I checked the docs multiple times and I can’t find the problem with what I did in the code ?
In the docs why this format didin’t work
const unixTimeZero = Date.parse('01 Jan 1970 00:00:00 GMT');

Any one had the same problem ? I l checked the old post on the forum and it seems that it happened quite a lot but with no solution

I didn’t understand your replay (the 3 points you mentioned) can you elaborate please

The API must accept any of the valid date strings that can be passed to JavaScript’s Date.parse() method.

const unixTimeZero = Date.parse('01 Jan 1970 00:00:00 GMT');

That’s not technically a valid format, look:

Those are the valid formats. You don’t use Date.parse directly, but the string it accepts as an argument defines what JS will accept as a valid date string.

What you’ve typed there is what JS’ Date will output as a string, but it isn’t valid input.

If you’d typed

const unixTimeZero = Date.parse('1970-01-01T00:00:00.000Z');

Then that is valid input, for example.


Having said that, there is the caveat that Date.parse does accept various other date strings, including in the format you’ve typed, but it’s completely implementation-dependent, JS’ Date stuff is really, really horrible. As far as I can see, what it wants you to do is allow the API to accept:

"1970-01-01"
"1970-01-01T00:00:00.000Z"
"1970-01-01T00:00:00.000+00:00"

Which will all definitely work with Date.parse regardless of what JS engine is being used.

but those dates are not valid and will show you the result 0 which is false , I kept looking for an answer but I’m stuck here

I mean, that’s a valid date string that can be parsed by Date.parse, and that’s specifically what the tests ask for.

And it should show 0 for the Unix timestamp: 0 seconds after Unix time starts is 0.

Finely after 10 H of searching I could pass your request but still the same message appears to me so I don’t think the problem is in those dates

"1970-01-01"
"1970-01-01T00:00:00.000Z"
"1970-01-01T00:00:00.000+00:00"

I apologise, I ignored the non-standard date strings when I was skim reading the requirements and docs, as I assumed the requirements meant a standardised date string.

So, anyway, just allow the function that parses the incoming date in your code to handle (eg) "01 Jan 1970 00:00:00 GMT" or "01 Jan 1970" or whatever, just any allowed date string. It just needs to check if the argument sent via the API, when ran through new Date, actually parses to a date.

I think your main issue is that you’ve got a very specific condition in your code that only allows some formats of date instead of just trying to parse the incoming date string and sending an error response if that fails – the input is going to either be a numeric timestamp or it’s going to be something that new Date (and by extension Date.parse, as that’s where the rules around what is or isn’t valid are defined) will parse, and if it’s neither then it’s not valid.

https://www.w3schools.com/js/js_date_formats.asp

You can give the tests a look to see what types of date strings are passed and the expected values.

This topic was automatically closed 182 days after the last reply. New replies are no longer allowed.