Timestamp Microservice Can't pass - Your project can handle dates that can be successfully parsed by new Date(date_string)

Tell us what’s happening:
Describe your issue in detail here.
I can’t pass the test for ‘Your project can handle dates that can be successfully parsed by new Date(date_string)’

Can’t understand what I’m doing wrong.

Your project link(s)

solution: https://replit.com/@1nfernox/boilerplate-project-timestamp

The code I have is:

let responseObject = {};
app.get('/api/:input', function(req, res) {
  let input = req.params.input;

  if(input.includes('-')){
    responseObject['unix'] = new Date(input).getTime()
    responseObject['utc'] = new Date(input).toUTCString()
  } else {
    input = parseInt(input)
    responseObject['unix'] = new Date(input).getTime()
    responseObject['utc'] = new Date(input).toUTCString()
  };

  if (/\d{5,}/.test(input)){
    input = parseInt(input);
    responseObject['unix'] = input;
    responseObject['utc'] = new Date(input).toUTCString();
  } else {
    responseObject['unix'] = new Date(input).getTime()
    responseObject['utc'] = new Date(input).toUTCString()
  };

  if(!responseObject['unix'] || !responseObject['utc']){
    res.json({error : "Invalid Date"})
  };

  res.json(responseObject);
});

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

  res.json(responseObject);
})

Your browser information:

User Agent is: Mozilla/5.0 (Windows NT 10.0; Win64; x64; rv:91.0) Gecko/20100101 Firefox/91.0

Challenge: Timestamp Microservice

Link to the challenge:

Welcome there,

I’ve edited your post for readability. When you enter a code block into a forum post, please precede it with a separate line of three backticks and follow it with a separate line of three backticks to make it easier to read.

You can also use the “preformatted text” tool in the editor (</>) to add backticks around text.

See this post to find the backtick on your keyboard.
Note: Backticks (`) are not single quotes (’).


It looks as though you have a lot more logic than needed. The hint is in the test user-story:

…can be successfully parsed by new Date(date_string)

That is, if this operation succeeds:

new Date(input)

Then, you do not need to return Invalid Date.

Hope this helps

Thank you for the help. I’ve now got it to pass by changing the code to:

let responseObject = {};
app.get('/api/:input', function(req, res) {
  let input = req.params.input;

  if(input.includes('-') || input.includes(' ')){
    responseObject['unix'] = new Date(input).getTime()
    responseObject['utc'] = new Date(input).toUTCString()
  } else {
    input = parseInt(input)
    responseObject['unix'] = new Date(input).getTime()
    responseObject['utc'] = new Date(input).toUTCString()
  };

  if(!responseObject['unix'] || !responseObject['utc']){
    res.json({error : "Invalid Date"})
  };

  res.json(responseObject);
});

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

  res.json(responseObject);
})

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