Backend project1: Timestamp microservice

Hi! I’m working on the timestamp microservice and I don’t know why my code doesn’t pass these 2 tests :

Your project can handle dates that can be successfully parsed by new Date(date_string)

A request to /api/1451001600000 should return {unix: 1451001600000, utc: “Fri, 25 Dec 2015 00:00:00 GMT”}

For the first test, the regex that I defined is this: /\d{4}-\d{0,2}-\d{0,2}|(\d{1,2}/\d{1,2}/\d{4})|(\s{3} \d{1,2} \d{4})|(\d{1,2} \s{3} \d{4})$/

For the second test, I changed the date of my machine but that didn’t fix the error.

Can someone please tell me where can be the problem.

Here is a link to my code: https://replit.com/@redouane1/boilerplate-project-timestamp#server.js

There are a few things going wrong.

  1. The responseObj object is never reinitialized. So whatever is set remains between calls to the route. Which may make debugging harder.

  2. Your try/catch in the timestampRegex.test(req.params.date) if/else is masking an error. Remove it and look at the console. When the date is the unix string you have to convert it to a number before passing it to new Date().

new Date(1451001600000);
// Fri Dec 25 2015 01:00:00 GMT+0100 (Central European Standard Time)

new Date('1451001600000');
// Invalid Date
  1. You are not handling the “05 October 2011, GMT” param string correctly. It may be easier to just use includes and look for a dash or a space date.includes('-') || date.includes(' ').

Hi
First, thanks for your reply!
For the date handling, I’m not sure if includes can handle all date formats but I will try.
For the timestamp, I tried to convert it but still the same problem and I
really couldn’t figure out why

This topic was automatically closed 182 days after the last reply. New replies are no longer allowed.