Back End Development and APIs Projects - Timestamp Microservice

Tell us what’s happening:

[SPOILER]
Code contains solution.

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

I have used this in my code multiple times yet the error occurs. Need Help!

let responseObject = {};

app.get('/api', (req, res) => {
  responseObject['unix'] = new Date().getTime();
  responseObject['utc'] = new Date().toUTCString();

  res.json(responseObject);
});

app.get('/api/:date', (req, res) => {
  let date = req.params.date;
  console.log(date);

  if(date.includes('-')){
    responseObject['unix'] = new Date(date).getTime();
    responseObject['utc'] = new Date(date).toUTCString();
    console.log(responseObject);
  } else {
    responseObject['unix'] = Number(date);
    responseObject['utc'] = new Date(Number(date)).toUTCString();
    console.log(responseObject);
  }
  
  if(!responseObject['unix'] || !responseObject['utc']) {
    res.json({error: 'Invalid Date'});
  }
  
  res.json(responseObject);
  
});

Your project link(s)

solution: boilerplate-project-timestamp (1) - Replit

Your browser information:

User Agent is: Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/108.0.0.0 Safari/537.36

Challenge: Back End Development and APIs Projects - Timestamp Microservice

Link to the challenge:

The following is a date that can be parsed by new Date():

05 October 2011

1 Like

I have done that. Here.
responseObject['unix'] = new Date(date).getTime();

I have tried this too.
responseObject['unix'] = new Date( '05 October 2011' ).getTime();

Not working.

Finally solved it! Thanks for your help.

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