Tell us what’s happening:
Can someone explain why isn’t my code passing the test - “It should return the expected error message for an invalid date” . I think i have tested it for all possible cases. Much appreciated.
Your code so far
app.get("/api/timestamp/", (req, res) => {
res.json({ unix: Date.now(), utc: Date() });
});
app.get("/api/timestamp/:date_string", (req, res) => {
let dateString = req.params.date_string;
//A 4 digit number is a valid ISO-8601 for the beginning of that year
//5 digits or more must be a unix time, until we reach a year 10,000 problem
if (/\d{5,}/.test(dateString)) {
// if(/(-)/.test(dateString)){
// res.json({ error: "Invaid Date" });
// }
const dateInt = parseInt(dateString);
//Date regards numbers as unix timestamps, strings are processed differently
res.json({ unix: dateString, utc: new Date(dateInt).toUTCString() });
}
let dateObject = new Date(dateString);
if (dateObject.toString() === "Invalid Date" ) {
res.json({ error: "Invaid Date" });
} else {
res.json({ unix: dateObject.valueOf(), utc: dateObject.toUTCString() });
}
});
Challenge: APIs and Microservices Projects - Timestamp Microservice
Link to the challenge: