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(),
});
}
});