Hello. I have a problem with passing a:
" Your project can handle dates that can be successfully parsed by `new Date(date_string)" in a Timestamp Microservice - Back End Development and APIs
I done my code in replit.
What i did:
app.get("/api/:date", (req,res)=>{
let date_string = req.params.date;
let odpowiedz = {}
if (date_string.includes("-")) {
odpowiedz[“unix”] = new Date(date_string).getTime();
odpowiedz[“utc”]= new Date(date_string).toUTCString();
} else {
date_string = parseInt(date_string),
odpowiedz[“unix”] = new Date(date_string).getTime();
odpowiedz[“utc”]= new Date(date_string).toUTCString();
}
if (!odpowiedz[“unix”] || !odpowiedz[“utc”]){
odpowiedz = {error : “Invalid Date”}
}
res.json(odpowiedz)
})
And I don’t know what to do next to pass it. Everything else is completed corretly
Please post a link to the code on repl.it, if possible, as it makes debugging API projects so much easier.
You should log the req.params.date at the top of your route and run the tests to see what all values are being input because there are valid dates parseable by new Date() that aren’t covered by your test cases.
Down I give you my solution which i had to add to pass it, but first I give you hint to do it by yourself.
Add some console.logs to show params and results. You will see which condisions you dont pass.
My code that I added to that above:
else if (!isNaN(date_string)){
date_string = parseInt(date_string),
odpowiedz[“unix”] = new Date(date_string).getTime();
odpowiedz[“utc”]= new Date(date_string).toUTCString();