Timestamp Microservice - parsing by new Date

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.

https://replit.com/@StefanMocek/timestamp-my-own

https://replit.com/@StefanMocek/timestamp-my-own

I did it on my own.
I added console.log to show how the fcc test runs it and why it didnt pass my project.
I had to add one more if statement.

But thanks for your commitment anyway :slight_smile:

I have the same problem.

How did you solve it?

@mrnilas Can you please create a new topic with your specific questions and a link to your code?

Thank you.

1 Like

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

1 Like

thx.

I find a solution by myself yesterday. I forgot to delete my question.