Timestamp Microservice - tests for retrieving current time fail

I am failing these two tests:

  • An empty date parameter should return the current time in a JSON object with a unix key
  • An empty date parameter should return the current time in a JSON object with a utc key

However, if I go the appropriate url, I seem to get a valid response:

{"utc":"Wed, 11 Jan 2023 03:27:54 GMT","unix":1673407674220}

Would appreciate it if someone could point out what is wrong with this response.

Edit: I was able to get the tests to pass if I hardcoded the current unix time instead of using Date.now(). Obviously this isn’t a satisfying solution, but I’m still at a loss as to how Date.now() could be incorrect.

Please post your Replit or a GitHub repo with the code.

So it is only the unix value that makes the test fail?

I know it is of type Number in the response you posted but are you sure that is what you are returning and not a String?

Here is the replit: fcc-backend-timestamp - Replit

The relevant code is at the bottom of index.js. Both tests mentioned in the post fail, and I was able to get both to pass by replacing the call to Date.now() with a hardcoded number for the current unix time on line 33.

I’m having the same issue!
Here’s the relevant code:

app.get("/api/:date?", (req, res) => {
  var date = req.params.date;

  if (date === undefined) {
    date = Date.now()
  } else if (parseInt(date).toString() == date) {
    date = parseInt(date);
  }

  date = new Date(date);

  if (date.toString() == "Invalid Date") {
    res.json({ error: "Invalid Date" });
  } else {
    res.json({
      unix: date.getTime(),
      utc: date.toUTCString()
    })
  }
});

I truly can’t see what’s wrong. I’m even logging out what node is sending back to fCC and it all seems perfectly alright.

@ancienthero did you manage to fix it?

Here’s my replit.

Your repl should pass. It does for me. It could be failing because your PC clock is a tiny bit out of sync. Try resyncing by going into your system Date and Time settings and finding the ‘sync’ option. Then, resubmit your repl link and see if it works.

1 Like

AA thank you very much! That solved it.