Hi folks,
I’m running into an issue with the final two tests for the Timestamp Microservice project.
-
An empty date parameter should return the current time in a JSON object with a
unix
key -
An empty date parameter should return the current time in a JSON object with a
utc
key
When I run my project locally or on repl.it, it appears to return the correct JSON object when an empty date string parameter is passed. I create a new date and use Date.getTime()
and Date.toUTCString()
to populate the unix and utc fields.
But the final two tests keep failing so I’m sure I’m missing something.
I’d be very grateful for any suggestions as to what I might be overlooking.
Links
My code
// If no timestamp is provided, return the current date
app.get("/api/timestamp", (req, res) => {
const date = new Date();
res.json({ unix: date.getTime(), utc: date.toUTCString() });
});
app.get("/api/timestamp/:date", (req, res) => {
const dateStr = req.params.date;
// If non-digit characters are passed, check if dateStr is a valid ISO-8601 date
if (/\D/.test(dateStr)) {
const ISODate = new Date(dateStr);
if (ISODate.toString() === "Invalid Date") {
res.json({ error: "Invalid Date" });
} else {
res.json({ unix: ISODate.valueOf(), utc: ISODate.toUTCString() });
}
} else {
// If only digits are passed, check if dateStr is a valid unix timestamp
const dateInt = parseInt(dateStr);
const UnixDate = new Date(dateInt);
if (UnixDate.toString() === "Invalid Date") {
res.json({ error: "Invalid Date" });
} else {
res.json({ unix: dateInt, utc: UnixDate.toUTCString() });
}
}
});