Timestamp Microservice - Tests failing on empty parameters

Hi folks,

I’m running into an issue with the final two tests for the Timestamp Microservice project.

  • 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

When I run my project locally or on repl.it, it appears to return the correct JSON object when an empty date string parameter is passed. I create a new date and use Date.getTime() and Date.toUTCString() to populate the unix and utc fields.

But the final two tests keep failing so I’m sure I’m missing something.

I’d be very grateful for any suggestions as to what I might be overlooking.

Links

live project
repo

My code

// If no timestamp is provided, return the current date
app.get("/api/timestamp", (req, res) => {
  const date = new Date();
  res.json({ unix: date.getTime(), utc: date.toUTCString() });
});

app.get("/api/timestamp/:date", (req, res) => {
  const dateStr = req.params.date;

  // If non-digit characters are passed, check if dateStr is a valid ISO-8601 date
  if (/\D/.test(dateStr)) {
    const ISODate = new Date(dateStr);
    if (ISODate.toString() === "Invalid Date") {
      res.json({ error: "Invalid Date" });
    } else {
      res.json({ unix: ISODate.valueOf(), utc: ISODate.toUTCString() });
    }
  } else {
    // If only digits are passed, check if dateStr is a valid unix timestamp
    const dateInt = parseInt(dateStr);
    const UnixDate = new Date(dateInt);
    if (UnixDate.toString() === "Invalid Date") {
      res.json({ error: "Invalid Date" });
    } else {
      res.json({ unix: dateInt, utc: UnixDate.toUTCString() });
    }
  }
});

Hi @GK-Hynes :wave: Welcome back to the freeCodeCamp forums—it’s been a while!

I just tried your service and ran the tests against it and they all seem to be passing now. Seems you may have figured it out, that’s great!

If you are still having a problem, please drop a note here and I’ll try to help out.

Thanks so much for having a look, @robertgroves. It’s good to be back working on FCC projects, and hopefully spending more time on the forum.

It looks like it might have been a caching issue, since the site has accepted my repl.it project on two other devices, just not my primary computer, even with cache cleared or in another browser. :man_shrugging:

I’m just relieved I wasn’t missing something really obvious in the code.

Thanks again.

1 Like