Back End Development and APIs Projects - Timestamp Microservice


This code validates to true no matter if the date is valid or not
const isValidDate = moment(isoFormattedDate).isValid();
// console.log(“isValidDate”, isValidDate)
if (isValidDate) {
res.json({ unix: Number(param), utc: utcTimeZone });

} else {
  res.status(400).json({ error: "Invalid date" });

I have tried with both 2023-02-30(invalid) and 2016-12-25(valid). Been chatting with the gpt, but cant figure it out. Any tips?

Your browser information:

User Agent is: Mozilla/5.0 (Macintosh; Intel Mac OS X 10_15_7) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/ Safari/537.36

Challenge: Back End Development and APIs Projects - Timestamp Microservice

Link to the challenge:

If the input date string is invalid, the API returns an object having the structure { error : "Invalid Date" }

  1. Your else that is for the invalid date is inside a condition that won’t allow it to handle the invalid date. Move it last, or have the function return that as the default value after all valid options have failed.

  2. If no date payload is provided you must construct one using the current date.

Edit: Also the route parameter should be optional some/route/:param?, or you need a separate /api route for it. If you use the browsers dev tools you can inspect the request/response.

Not sure about your original question as your Replit no longer has that code and the code you posted isn’t enough to go on. I might also reconsider the lib anyway unless you really need it.