Timestamp help please

I cant pass the timestampe test because of the challenge that states handling parsed dates.

below is code that passes everything except for that challenge.


app.get('/api/:date?', (req, res)=>{
  if(req.params.date == null){
    const nowdate = new Date().toUTCString()
    res.json({unix: Date.parse(nowdate), utc: nowdate})
  } else if (!req.params.date.match(/-/g)){
    const parsed = parseInt(req.params.date)
  res.json({unix: parsed, utc: new Date(parsed).toUTCString()})
  } else {
  const paramdate = new Date(req.params.date);
    if(paramdate == 'Invalid Date'){
      res.json({error: 'Invalid Date'})
    } else {
      const myDate = new Date(req.params.date);
    const gmtDate = myDate.toUTCString();
    const otherDate = Date.parse(myDate);
    res.json({utc: gmtDate, unix: otherDate});
    }
  }
})

if i take out where i use regex and parse a unix number in the url, it passes the above but fails obviously for not returning the unix number.


app.get('/api/:date?', (req, res)=>{
  if(req.params.date == null){
    const nowdate = new Date().toUTCString()
    res.json({unix: Date.parse(nowdate), utc: nowdate})
  }  else {
  const paramdate = new Date(req.params.date);
    if(paramdate == 'Invalid Date'){
      res.json({error: 'Invalid Date'})
    } else {
      const myDate = new Date(req.params.date);
    const gmtDate = myDate.toUTCString();
    const otherDate = Date.parse(myDate);
    res.json({utc: gmtDate, unix: otherDate});
    }
  }
})

So i am assuming it has something to do with how i am either parsing or searching for the unix number from the url. This is the 3rd attempt at building this, after passing 3 of the other backend tests.

https://replit.com/@RobertWisnewski/boilerplate-project-timestamp#index.js

For this request:

https://boilerplate-project-timestamp.robertwisnewski.repl.co/api/05%20October%202011,%20GMT

You give this response:

{"unix":5,"utc":"Thu, 01 Jan 1970 00:00:00 GMT"}

Try logging the req.params.date and parsed inside the else if (!req.params.date.match(/-/g)) statment.

1 Like

i see the error now but struggling on what is causing it. Is the request a unix ‘5’ or is the request ‘05%20October%202011,%20GMT’ . If its the latter, isnt that an invalid date? so i would of thought it would catch in my invalid date if statement.

sorry, just truggling understanding

No the date string with spaces should be accepted as a valid date. I believe you just handle it the same way as you do the dash separated date.

new Date("05 October 2011, GMT")
// Wed Oct 05 2011 02:00:00 GMT+0200 (Central European Summer Time)

i got it. thank you so much. dates send my head for a loop.

(!req.params.date.match(/-|\D/g)){
    const parsed = parseInt(req.params.date)
    console.log(parsed)
    console.log(req.params.date)
  res.json({unix: parsed, utc: new Date(parsed).toUTCString()})

thank you so much.

im down to the logs of the exercise tracker and im done with backend development