Timestamp Microservice - Failed test on invalid date


I’m doing the timestamp microservice challenge of the Back End course. At first I was having trouble understanding how the Data object works in Javascript, but I managed to work it out. I’ll drop a screenshot of my code on VSCode

As you can see I created two endpoints, one that works with Unix timestamp provided by the challenge to pass one of the tests (I had no problem with that one) and the second wich I called the main one.
In this endpoint I created two variables, one to catch the input and another to work with the current time (pardon my english for calling it “nowDate”). This is where things became tricky for me. Using conditionals I ask if the input is undefined, meaning that the user didn’t write any date so I got to work with the current time, if the user write something then I ask if the input have blanks or hyphens (whether the date is formatted YYYY-MM-DD or “Dec 10 1997”), if everything fails then I drop the response object with the error.

My problem is when testing manually with every combination I could think, I found no problem, but when uploading the links on the challenge pages every test past except for this one

Funny thing is I got that test passed when worked with the part of my code that is commented, as you can see I used a regular expression to force the input to be “YYYY-MM-DD” and got everything right except for this test

So I assumed that the correct approach was to get the date to work with the Date object parser and manage to pull out the first sort-of-solution

I’ll drop both the github link and the replit link of my project so you can get a better look at it

https://Timestamp-service.darc-7.repl.co (live app)
githubLink: GitHub - darc-7/Timestamp-service

Thank you for your time!
Your browser information:

User Agent is: Mozilla/5.0 (Windows NT 6.1) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/91.0.4472.101 Safari/537.36

Challenge: Back End Development and APIs Projects - Timestamp Microservice

Link to the challenge:

For the following test:

If the input date string is invalid, the api returns an object having the structure { error : "Invalid Date" }

You can try using this:

The new Date(input_string) returns an invalid value when the input_string doesn’t represent a valid value. And the toString() method on it returns a "Invalid Date" string. For example, the following three statements return an “Invalid Date” as result:

(new Date("2022-90-90")).toString()
(new Date("12345678901234567890")).toString()
(new Date("abcd-xx-yy")).toString()
1 Like

Sorry but I don’t understand your answer.
I thought that I used the toString() metod in the correct way when asking if the input includes either hyphens or blanks.
I’m starting to think that maybe there’s something wrong with the way the if statements are arranged

This string includes hyphens: "2022-33-33" , and is it a valid date?

How would your code (from your GitHub link) work and what kind of result does it produce?

let reqDate = req.params.date;
// ...
else if (reqDate.toString().includes('-') || reqDate.toString().includes(' ')) {
    reqDate = new Date(req.params.date);|
    res.json({unix: reqDate.getTime(), utc: reqDate.toUTCString()});|
1 Like

After testing now I can notice the issue, the output is
Which doesn’t match the output of the challenge.
I tried doing this

else if (reqDate.toString().includes('-') || reqDate.toString().includes(' ')){
      reqDate = new Date(req.params.date);
      if(reqDate.getTime() === null){
        res.json({ error : "Invalid Date" });
      res.json({unix: reqDate.getTime(), utc: reqDate.toUTCString()}); 

But it doesn’t seem to work

What is the value in the reqDate ? (You can console.log it and see the value).

1 Like

When using console.log the value that I get is Invalid Date so I tried this

else if (reqDate.toString().includes('-') || reqDate.toString().includes(' ')){
    reqDate = new Date(req.params.date);
    if(reqDate.toUTCString() === 'Invalid Date'){
      res.json({ error : "Invalid Date" });
    res.json({unix: reqDate.getTime(), utc: reqDate.toUTCString()}); 

And seems to work, but now I get this error in the console

Try adding a return to the two res you have.

1 Like

I ended up submitting the project and it passed all the tests (instead of using Repl, I ended up using Heroku), but I still don’t know why I got that HTTP Headers error, why that happened?

Thank you for your time and answers, I was struggling to understand how the Date object works.

It happens if you send more than one response for a single request. Using a return should fix it.

1 Like

Date is one of the most important data types in the JS programming language - and it is used extensively in the apps and databases. In general, documentation has most of the related information and few examples. But, actual work scenarios are various and one has to figure by trial and error. Date comparison, conversion, date arithmetic are the most common challenges and also date validation and time zones.

1 Like

Thank you providing that link, it helped me to understand better.

I’ve marked one of your posts as the solution of this topic. Thank you again for your time and patience.

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