A request to /api/timestamp/:date? with a valid date should return a JSON object

Sup guys. I need some help here please. I’m not understanding quite well why the challenge of building a timestamp project. It responds with: “A request to /api/timestamp/:date? with a valid date should return a JSON object with a unix key that is a Unix timestamp of the input date in milliseconds”
Though as you can check in my demo: https://boilerplate-project-timestamp.alejogarcia1.repl.co/ when I ask for a date in the url it returns me a json with unix key in ms.

Heres part of the code:

app.get("/api/timestamp/:date_str?", function(req, res){
  const { date_str } = req.params;

  console.log(date_str)

  let date;

  if (!date) {
    date = new Date();
  } else {
    date = new Date(date_str)
  }
    return res.json({
      unix: date.getTime(),
      utc: date.toUTCString()
    })

})

Challenge: Timestamp Microservice

Link to the challenge:

Try and look at the example provided and see how the results are different then your own.

The problem is in your if statement. Your “let date” is undefined which will return false, but because you flipped it by putting an “!” before it, it returns true. So it will always return the current date instead of using the date_str as you intended. You actually don’t need the if statement for the purpose you’re using it, because a new date object should be created every time the user visits that url.

1 Like