Tell us what’s happening:
I tried to make it without Moment JS and it’s working.
https://kaput-join.glitch.me/api/timestamp/1539129600
https://kaput-join.glitch.me/api/timestamp/2015-10-10
I think, I should add some RegExp and 404 route. But I don’t know RegExp yet.
Are there any bad practices in the code? Thank you!
Your code so far
app.get('/api/timestamp/:date', function(req, res) {
// check, if entry is yyyy-mm--dd
var data = req.params.date;
if (isNaN(data)) {
// if entry is yy-mm-dd
var unixFormat = new Date(data).getTime()/1000;
var temp = new Date(unixFormat * 1000);
var utc = temp.toUTCString();
}
else {
// if entry is unix time
var temp = new Date(data * 1000);
var utc = temp.toUTCString();
var unixFormat = parseInt(data); // for removing quotation marks
}
res.json({
unix: unixFormat,
utc: utc
});
})
app.get('/api/timestamp', function(req, res) {
// Now
var time = new Date();
var utc = time.toUTCString()
var unix = time.getTime();
res.json({
unix: unix,
utc: utc
});
})
Your browser information:
User Agent is: Mozilla/5.0 (Macintosh; Intel Mac OS X 10_13_6) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/69.0.3497.100 Safari/537.36
.
Link to the challenge:
https://learn.freecodecamp.org/apis-and-microservices/apis-and-microservices-projects/timestamp-microservice