3

I'm attempting to convert a Date String to a unix timestamp in Node.js.

My code below works perfectly on my client but when I run it on my server I get an error:

(node:19260) UnhandledPromiseRejectionWarning: Unhandled promise rejection (rejection id: 1): TypeError: input.substring is not a function

My code:

function dateParser(input) {
    // function is passed a date and parses it to create a unix timestamp

    // removing the '.000' from input
    let finalDate = input.substring(0, input.length - 4);
    return new Date(finalDate.split(' ').join('T')).getTime();
}

Example of my input would be 2017-09-15 00:00:00.000

So why does the above work on my client but not in Node, and how would I duplicate the functionality in node?

4
  • How is dateParser() being called and what is being passed to it? Commented Sep 15, 2017 at 10:48
  • Could you console.log(typeof input) ? Commented Sep 15, 2017 at 10:49
  • @TGrif object is returned Commented Sep 15, 2017 at 10:49
  • 1
    As you're not passing a string but a date object to your dateParser function, you get a input.substring is not a function error. You could use Date.parse() to get the Unix timestamp representation of the date. Commented Sep 15, 2017 at 12:03

4 Answers 4

21

Create a date object from your input DateTime string and then use getTime() and divide the result with 1000 to get the UNIX timestamp.

var unixTimestamp = Math.floor(new Date("2017-09-15 00:00:00.000").getTime()/1000);
console.log(unixTimestamp);

Sign up to request clarification or add additional context in comments.

2 Comments

For anybody converting the current time to a unix timestamp you should use Math.floor, otherwise you'll be generating a time ever so slightly in the future. In fact, I think Math.floor is always more appropriate as it's usually desirable to discard the precision.
For time, definitely .floor as RichardScarott says. It's completely commonplace in massive sync systems that it would make a difference and trip a sync algorithm the wrong way.
3

I will recommend using momentjs to handle the dates. Using momentjs you could do:

moment().unix(); // Gives UNIX timestamp

If you already have a date and want to get the UNIX timestamp relative to that date, you could do:

moment("2017-09-15 00:00:00.000").unix(); // I have passed the date that will be your input 
// Gives out 1505413800

It gets very productive when handling date/time using momentjs.

Comments

0

A Unix timestamp is the number of seconds from a specific date. The Javascript function getTime() returns the number of milliseconds from the same specific date til the date you specified.

So, if you divide the result of your function by number 1000 you will get the Unix timestamp and convert from milliseconds to seconds. Don't forget to ignore decimal places.

The error message you are receiving is because the value of input is not a string.

Comments

0

2 options with slightly different results if system timezone is not set to UTC

Option 1 - Ignores system timezone

console.log(Math.floor(new Date("2020-01-01").getTime()/1000));

> 1577836800

Option 2 ("moment.js") - Timestamp will differ with system timezone

console.log(moment('2020-01-01').unix());

> 1577854800

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.