Timestamp Microservice cant pass two test

Hi folks,
I am working on Timestamp Microservice ,but cant pass the ** A request to /api/: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** and ** Your project can handle dates that can be successfully parsed by new Date(date_string)**. Actually my get request return a unix key, and I am parsing the dates with new Date(). Here is my heroku link. heroku-link. Also here is my code that cant pass the tests.

app.get('/api/:date', (req, res) => {
  const moment = require('moment');
  const date_string = req.params.date;

  if (moment(date_string, 'X', true).isValid()) {
    res.json({
      unix: JSON.parse(date_string),
      utc: new Date(Number(date_string)).toUTCString(),
    });
  } else if (moment(date_string, ['DD-MM-YYYY', 'D-M-YYYY'], true).isValid()) {
    res.json({
      unix: new Date(date_string).getTime() / 10,
      utc: new Date(date_string).toUTCString(),
    });
  } else if (
    moment(date_string, 'dddd, DD MMMM YYYY, h:mm:ss GMT', true).isValid()
  ) {
    res.json({
      unix: new Date(date_string).getTime() / 1000,
      utc: date_string,
    });
  } else if (!moment(date_string).isValid()) {
    res.json({ error: 'Invalid Date' });
  } else {
    res.json({
      unix: new Date(date_string).getTime() / 10,
      utc: new Date(date_string).toUTCString(),
    });
  }
});

It’s hard to debug with just part of the code and not a live link to a project on repl.it or glitch. To debug this, you should log the route inputs (console.log(req.params);, etc.) and all possible responses (console.log({...}); before every return res.json({...});) and then run the fCC tests against the project and compare the logging output with what the tests expect and you should see a valid time that is marked invalid or vice-versa or some other problem. Additionally, you should return responses (return res.json({...});) because if you don’t your code will continue execution and may send additional unexpected responses.

Somewhere in the project or at least in the forums was a hint that new Date() would handle everything, so using moment is overkill and may actually be contributing to the problem.

The tests mention that the unix timestamp should be provided in milliseconds. .getTime() already returns milliseconds.

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