I have been working on this specific project on freecodecamp.org and I am determined to complete it without using the moment.js library for assistance:
So far, everything seems to be functioning properly except for one scenario. When the date is formatted in ISO-8601 like this
GET [project url]/api/timestamp/2015-12-25
app.get("/api/timestamp/:date_string?",function(req,res){
var date,
result = {unix : null, utc: null},
dateParameter = req.params.date_string;
console.log("dateParameter:", dateParameter);
if(dateParameter === ''){
date = new Date();
result.unix = date.getTime()*1000;
result.utc = date.toUTCString();
}
else{
try{
console.log("date_string:", date.dateParameter);
var dateNumber = parseInt(dateParameter)*1000; // convertion from seconds to milisecond
date = new Date ( +dateNumber);
console.log("outer try date:", date.getTime());
}catch(err){
try{
date = new Date(dateParameter + "T00:00:00");
console.log("inner try date:", date.getTime());
}catch(err){
result.unix = null;
result.utc = "Invalid Date";
res.json(result);
}
}finally{
console.log("date:", date.getTime());
result.unix = date.getTime()*1000;
result.utc = date.toUTCString();
res.json(result);
}
}
});
However, upon running the code, I am encountering an unexpected outcome of date: NaN
. Can anyone spot what mistake I might be making here?