Stuck Horribly on Creating a Timestamp Service

Hi Everyone

So, what should have been a 30-min challenge has now been going on for 3 hours, and I am not getting anywhere. I have explored the Date object and methods back and forth, and have tried so many things, and nothing works. I don’t know what I am doing wrong here, but virtually all tests keep failing.

Even differentiating between valid and invalid time parameters isn’t working. Maybe it’s just one of those days, but I’d really appreciate some guidance.

Here is the replit: boilerplate-project-timestamp - Replit
And here is the relevant function (line 28 on the replit):

app.get("/api/:date", (req, res) => {
  console.log(req.params.date);
  let d = new Date(req.params.date);
  console.log(d);
  
//if the date object returned as valid
  if (d !== "Invalid Date") {
    console.log("json");
    return res.status(200).
      json({
        unix: Date.parse(d),
        utc: d.toUTCString()
      });      
  };

//if the date object is invalid, but the params are a valid number (ie timestamp)
  if (Number(req.params.date)) {
    d = Number(req.params.date);
    return res.status(200)
          .json({
            unix: d,
            utc: new Date(d).toUTCString()
          })
  };

//invalid timestamp 
    return res.status(400)
            .json({
              error : "Invalid Date"
            });
});

Thank you!

Not sure if you fixed it but your code is passing for me. Are you submitting the correct URL?

https://boilerplate-project-timestamp.idanlib.repl.co

If all the tests are failing for you it might be an issue with Replit. Look in the browser console and network tab for any errors.

Hey man, thanks for trying it out. It’s amazing to learn that it worked for you when I got stuck for way too long. Perhaps it really was something with replit (though I did submit the correct url as you specified above).

In the end, I did get it to pass - the issue was with the second if. I needed to add .toString() to the date object:

if (d.toString() !== "Invalid Date")
instead of 
if (d!== "Invalid Date")

It’s a subtle difference, but it taught me the importance of (1) carefully, carefully reading the documentation, and (2) switching to Typescript as soon as possible.