Timestamp Microservice false positive?

Tell us what’s happening:
As far as I can tell this should be passing. The only test it is failing on is

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

but that is the exact response I am getting from my api. Is it a formatting error?

Here’s that function:

app.get('/api/:date?', (req, res) => {
  let date = req.params.date
  if (!date) {
      let currTimestamp = Date.now()
      let utc = (new Date(currTimestamp)).toUTCString()
      let unix = Date.now()
      res.json({
          unix,
          utc
      })
  }
  if(date){
      let isNumber = Number(date)
      let timestamp = new Date(date)
      
      if(timestamp == 'Invalid Date' && isNaN(isNumber)){
          res.json({
              "error": "Invalid Date"
          })
      } else if (isNaN(timestamp) == false) {
          let d = new Date(timestamp)
          let unixDate = new Date(d)
          let unix = unixDate.getTime()
          let utc = (new Date(timestamp)).toUTCString()
          res.json({
              unix,
              utc
          })
      } else if(typeof isNumber == 'number' && timestamp == 'Invalid Date'){
          let convertedDate = new Date(date * 1)
          let utc = convertedDate.toUTCString()
          res.json({
              "unix": date,
              utc
          })
      }
  }
})

Your project link(s)

solution: https://replit.com/@andrew-castro/boilerplate-project-timestamp

Your browser information:

User Agent is: Mozilla/5.0 (Macintosh; Intel Mac OS X 10_15_7) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/94.0.4606.71 Safari/537.36

Challenge: Timestamp Microservice

Link to the challenge:

You’ve got to log those responses before you send them. When you do, the failing test gets a

{ unix: '1451001600000', utc: 'Fri, 25 Dec 2015 00:00:00 GMT' }

Notice how it returns a string instead of an integer. The others don’t do that. The problem is here:

      } else if(typeof isNumber == 'number' && timestamp == 'Invalid Date'){
          let convertedDate = new Date(date * 1)
          let utc = convertedDate.toUTCString()
          console.log({
              "unix": date,
              utc
          })
          return res.json({
              "unix": date,
              utc
          })
      }

I didn’t think of logging it since I could see the ‘proper’ response in the browser. Thanks for the tip, that was helpful and I’ll be sure to log my responses before sending them in the future to double check the data.

For anyone who may run into a similar issue, I just had to parse the string into a number.

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