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?
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);