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
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()
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
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.
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.