I’ve completed all the task except for the empty parameter.
Here is my code:
app.get(“/api/timestamp/”, (req, res) => {
const date = new Date();
res.json({
unix:date.getTime(),
utc:date.toUTCString()
});
});
Or…
app.get(“/api/timestamp/”, (req, res) => {
res.json({
unix: Date.now(),
utc: Date() })
});
I tried both method above but still doesn’t work.
Here’s the rest of the code:
// this tasks is complete
app.get(“/api/timestamp/:date_slug”, (req, res) => {
const { date_slug } = req.params;
const date = new Date(date_slug);
const timeStamp = Number(date_slug)
const timeStampToDate = new Date(timeStamp)if(timeStampToDate.toUTCString() !== “Invalid Date”){
return res.json({
unix:timeStamp,
utc:timeStampToDate.toUTCString()
});
}if (date.toUTCString() === “Invalid Date”) {
return res.json({
error: “Invalid Date”
});
} else {
res.json({
unix: date.getTime(),
utc: date.toUTCString()
});
}
});
Problem:
// running tests
It should handle an empty date parameter, and return the current time in unix format
It should handle an empty date parameter, and return the current time in UTC format
// tests completed
Link to the challenge:
[https://www.freecodecamp.org/learn/apis-and-microservices/apis-and-microservices-projects/timestamp-microservice]