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

let responseObj={};

//timestamp
app.get('/api/:date?', function(req,res){
  let date = req.params.date;
  let integerReg= /^\d+$/;
  if (date.includes('-')){
    responseObj['unix']= new Date(date).getTime();
    responseObj['utc']= new Date(date).toUTCString();
  } else if (integerReg.test(date)) {
    date= parseInt(date);
    responseObj['unix']= new Date(date).getTime();
    responseObj['utc']= new Date(date).toUTCString();
  } else {
    responseObj['unix']= new Date(date).getTime();
    responseObj['utc']= new Date(date).toUTCString();
  }
  if(!responseObj.unix || !responseObj.utc){
    res.json({error: 'Invalid Date'});
  }

  res.json(responseObj);
});

app.get('/api', function(req,res){
  res.json({'unix': new Date().getTime(),
'utc': new Date().toUTCString()});
});

I’m stuck to pass this test , can anyone offer me a help?

1 Like

It is probably due to timezone difference.

When a simple date string is parsed it is timezone aware.
For example for me:

new Date("25 December 2021")
// 'Fri, 24 Dec 2021 23:00:00 GMT'

Since I know the timezone difference between UTC and my local timezone I can “manually” add the hours myself:

date.setHours(date.getHours() + <timezone_diff>)

But mind that this is a very fragile implementation that can be easily broken.
That’s why when working with Dates you will find mostly unambiguous definitions to avoid such errors and confusions.

For example take a look at how Date.UTC works in JS.

Hope this helps.

1 Like

This problem is solved by deploy this app to heroku

This section of your code helped me pass the test.
Thank you!