Timestamp project what is wrong?

All my code is working but not pass the challenge?

app.get('/api/:date',(req,res) => {
  const date = new Date(parseInt(req.params.date))
  if (date  == 'Invalid Date') { 
    res.json({ error : "Invalid Date" });
  }else{
    res.json({ unix: date.valueOf(), utc: date.toUTCString()});
  };
});

app.get('/api/',(req,res) => {
  if(!Object.keys(req.params).length) {
    const date = new Date(Date.now());
    res.json({ unix: date.valueOf(), utc: date.toUTCString()});
  }
});


best regard```

First, it’s easiest to post a link to a repl on repl.it and if that’s not possible, use the code block button (</>) for code so that copy/paste works.

You’re second route will be a problem regardless since if the if is not taken there’s no defined return or response, so that will not fit the spec. Log all the route inputs and responses and run the tests and you’ll get

req.params: {"date":"2016-12-25"}
req.query: {}
{ unix: 2016, utc: 'Thu, 01 Jan 1970 00:00:02 GMT' }

req.body: undefined
req.params: {"date":"2016-12-25"}
req.query: {}
{ unix: 2016, utc: 'Thu, 01 Jan 1970 00:00:02 GMT' }

req.body: undefined
req.params: {"date":"1451001600000"}
req.query: {}
{ unix: 1451001600000, utc: 'Fri, 25 Dec 2015 00:00:00 GMT' }

req.body: undefined
req.params: {"date":"05 October 2011, GMT"}
req.query: {}
{ unix: 5, utc: 'Thu, 01 Jan 1970 00:00:00 GMT' }

req.body: undefined
req.params: {"date":"this-is-not-a-date"}
req.query: {}
{ error: 'Invalid Date' }

req.body: undefined
req.params: {}
req.query: {}
{ unix: 1651877426234, utc: 'Fri, 06 May 2022 22:50:26 GMT' }

req.body: undefined
req.params: {}
req.query: {}
{ unix: 1651877426306, utc: 'Fri, 06 May 2022 22:50:26 GMT' }

All those 1970 dates are the test failures. Not all dates are sent as integers.

1 Like

Hi Jeremy and thanks so much!

The code format is better I hope is ok for you now .

Can you please let know why unix key is a Year Date and not is Date completely?

In the spectification do not say anything about this?

req.body: undefined
req.params: {"date":"2016-12-25"}
req.query: {}
{ unix: 2016, utc: 'Thu, 01 Jan 1970 00:00:02 GMT' }

req.body: undefined
req.params: {"date":"05 October 2011, GMT"}
req.query: {}
{ unix: 5, utc: 'Thu, 01 Jan 1970 00:00:00 GMT' }

best regard

1 Like

It says you have to be able to parse anything that Date() can parse and that covers these examples.

You’re trying to parse everything as integers, then dates, so the code is grabbing the 2016 and the 05 from the inputs and not parsing 2016-12-25 and 05 October 2011 with Date().


Ok my friend I will redesing  my code

Best
Hi Jeremy 

I  give to you my thanks , I pass my timestamp project.

best