How To Verify Milliseconds

Here’s the code

app.get('/api/:date', (req,res) => {
let time = req.params.date;
let date = new Date(time);
if (!time.includes('-') && time.length >= 6) time = +time
/*
Above: I've tried several methods of regex (\d{5,}) as well as the code I
wrote above, and I'm really lost on what to do. It just seems as 
easy as seeing if req.params.date is a long number (a string), and
then parsing time
*/
if (date.toUTCString() === "Invalid Date") res.json({error: date.toUTCString()})

  res.json({unix: date.valueOf(), utc: date.toUTCString()})
}) 

Is this for an FCC challenge? If so, can you provide a link? If not, can you provide a link to your project?

I assume it is the timestamp project.

https://www.freecodecamp.org/learn/back-end-development-and-apis/back-end-development-and-apis-projects/timestamp-microservice


You have turned it into a number but you are not using it.

You can convert the string to a number and pass that to the date constructor. Then call toUTCString on that date for the utc value and call getTime (or just use the number value after the conversion) for the unix value (which has to be of type number in the response).

1 Like

So something like this?

app.get('/api/:date', (req,res) => {
let time = req.params.date;
let date = new Date(Number(time));
if (!time.includes('-') && time.length >= 6) time = +time

if (date.toUTCString() === "Invalid Date") res.json({error: date.toUTCString()})

  res.json({unix: date.valueOf(), utc: date.toUTCString()})
}) 

Also, I’ve already done the test case where the date is invalid, I am talking abouit when the :date provided is in miliseconds, so I’m not sure why changing let date would affect the following

if (!time.includes('-') && time.length >= 6) time = +time

I was just saying that converting the string to a number and then not using it doesn’t make much sense.

let date = new Date(Number(time));

You can’t just blindly convert the string parameter to a number, that only works for one of the payloads (data passed to the parameter).


Log out the parameter and look at what is given as the payload. Then think about what happens when you use each of the payloads with the date constructor.

'1451001600000' once converted to a number is a valid format for the date constructor

new Date('1451001600000');
Invalid Date

new Date(1451001600000)
Fri Dec 25 2015 01:00:00 GMT+0100 (Central European Standard Time)

new Date(1451001600000).toUTCString()
'Fri, 25 Dec 2015 00:00:00 GMT'

new Date(1451001600000).getTime();
1451001600000

Thank you for your response! I appreciate your help.

I decided to not convert it into a number, and rather have it stay as a string.

Code:

app.get('/api/:date', (req,res) => {
let time = req.params.date;
let date = new Date((time));
if ((time).match(/\d{5,}/)) 
  time = +time

if (date.toUTCString() === "Invalid Date") res.json({error: date.toUTCString()})

  res.json({unix: date.valueOf(), utc: date.toUTCString()})
}) 

This however gives me an error

Cannot set headers after they are sent to the client

Try adding a return to your res.json calls return res.json({}).


As I said, '1451001600000' is a valid value to pass to the date constructor but only after converting it to a number. If you use it directly with the constructor it will give “Invalid Date”. Which it isn’t, it just isn’t the correct type. It has to be a number for the constructor to use it.

I decided to not convert it to a number, and rather us regex to cerify the string. I do however get an error, which is this:

Cannot set headers after they are sent to the client

That mostly happens when you have sent a response (e.g., res.send('Some message')) and after that somehow you are trying/sending another response. Put, a return after the first response, and the message will go away.

Like I said.

And again. You have to use the payloads to construct the dates. I don’t see how a regex is going to help with that.

Here is my code:

app.get('/api/:date', (req,res) => {
let time = req.params.date;
let date = new Date(parseInt(time));

  if (!time.match(/\d{5,}/)){
  time = +time
}

if (date.toUTCString() === "Invalid Date") {
  return res.json({error: date.toUTCString()})
}

  res.json({unix: date.valueOf(), utc: date.toUTCString()})
}) 

I successfully constructed it, but a requirement is that

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

Thus, am I forced to just use regex and strings?

What is the point of converting time to a number if you are not using the variable for anything after the conversion?


  1. Log out the date params when you submit and look at the different strings (the payloads) you are given.

  2. Try each one with the data constructor.

  3. Can you convert all the payloads to a number?

  4. The requirement is simply saying if the payload is valid for use with the date constructor it should work and the response should contain the expected values.