Back End Development and APIs Projects - Timestamp Microservice

Tell us what’s happening:
When i try my code on my device it works but when i submit it, it don’t pass the tests. Idk what is happening…

Your code so far

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

  const dateNumber = parseInt(date);

  const dateObject = new Date(dateNumber);

  const utcString = dateObject.toUTCString();

  if (date === undefined) {
    date = new Date().toString();
    res.json({ unix: date, utc: date });
  }
  if (utcString === "Invalid Date") {
    res.json({ error: "Invalid Date" });
  }

  res.json({ unix: date, utc: utcString });
});

Your browser information:

am using chrome

Challenge: Back End Development and APIs Projects - Timestamp Microservice

Link to the challenge:

Hello. Can you tell us which tests fail? It might help figure out where you went wrong.

Please post a Replit instead so people can more easily test the code.


  • Your unix date isn’t correct. It is in three different formats and even the one that is in the correct format is not in the correct data type (Number).

  • Your utc date isn’t correct except for the /api/1451001600000 route.

Use the devtools to inspect the request/response.


unix: Wrong format
utc: Wrong date

Request URL: /api/2016-12-25
Response: {"unix":"2016-12-25","utc":"Thu, 01 Jan 1970 00:00:02 GMT"}

Correct format, correct date, wrong data type for unix.

Request URL: /api/1451001600000
Response: {"unix":"1451001600000","utc":"Fri, 25 Dec 2015 00:00:00 GMT"}

unix: Wrong format
utc: Wrong date

Request URL: /api/05%20October%202011,%20GMT
Response: {"unix":"05 October 2011, GMT","utc":"Thu, 01 Jan 1970 00:00:00 GMT"}

unix: Wrong format
utc: Correct date

Request URL: /api
Response: { "unix": "Fri Oct 06 2023 17:39:03 GMT+0000 (Coordinated Universal Time)", "utc": "Fri Oct 06 2023 17:39:03 GMT+0000 (Coordinated Universal Time)" }

yeah man you are definitely right tnxs
I corrected the code like this

app.get('/api/:date?', (req, res) => {
  var date = req.params.date;
  
  var dateN, unixTimestamp;
  
  if (/^\d+$/.test(date)) {
    unixTimestamp = Number(date);
  } else {
    dateN = new Date(date);
    unixTimestamp = dateN.getTime();
  }
  const dateObject = new Date(unixTimestamp);

  const utcString = dateObject.toUTCString();

  if (date === undefined) {
    date = new Date();
    res.json({ unix: date.getTime(), utc: date });
  }
  if (utcString === "Invalid Date") {
    res.json({ error: "Invalid Date" });
  }

  res.json({ unix: unixTimestamp, utc: utcString });
});

The replit link

and it works…

tnxs once again…

This topic was automatically closed 182 days after the last reply. New replies are no longer allowed.