Arbitrary criteria preventing me from passing Back-End Course project

I am working on the Back-End Development And APIs course on FCC, which I’ve found to be an absolute boon to my programming skills. I spent a couple of hours on the first project today, and managed to pass all the criteria except one: "Your project can handle dates that can be successfully parsed by new Date(date_string)". This has completely stumped me. I’ve taken it to mean that my project is somehow rejecting a specific date input, but I’ve tested it multiple times for each date FCC sends to it and found it to work just fine. I did, after all, meet all the other criteria. I then went to the MDN docs to attempt to debug my program by inputting as many arbitrary dates into it as possible. It still worked fine. I just don’t understand. Is there a date that isn’t being parsed properly? My testing so far shows it to be working just fine. Does ES6 or NodeJS have a better date handling implementation than the built-in Javascript Date object? I might end up having to use that. Thanks!

Your project link(s)


Your browser information:

User Agent is: Mozilla/5.0 (Windows NT 10.0; Win64; x64; rv:96.0) Gecko/20100101 Firefox/96.0

Challenge: Timestamp Microservice

Link to the challenge:

It is not handling the input dates correctly. Log the input and output for all the tests and you’ll see the problem here:

req.body: undefined
req.params: {"date":"05 October 2011, GMT"}
req.query: {}
05 October 2011, GMT
{ unix: 1317772800000, utc: 'Wed, 5 Oct 2011 00:00:00 GMT' }

where the utc property is not in the correct format.

The problem is that you are doing more work than you need. new Date(input) will handle all valid date strings, and if not, provides a handy error. There are methods on Date() objects that return the date as a unix time or a UTC time string. MDN has the details. So let new Date(input) do the parsing (there are two special cases to account for) and use the methods to generate the unix/utc properties.

Agh! I’ve figured it out, I was missing the toUTCString() Date method. I was trying to manually convert an arbitrary date from a Date object into both a UTC and UNIX timestamp using a bunch of ternary operators designed to handle a bunch of obscure cases. I guess I just didn’t see the toUTCString() when browsing the MDN docs. It has simplified my code by about 10 lines and allowed me to pass the project; functional programming is the best. In hindsight, I should have done more research instead of rushing into the project without proper analysis of the task at hand. Thank you very much, have a great day!

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