Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Convert integer to Date in node.js

I'm trying to convert a integer to a Date using node .js, and Date.

I know that this is a very common question but all the solutions that have been posted before have failed to help me.

I am getting the dates from a json file found at http://api.guardian.gg/chart/elo/4611686018432537994,

Example date: 1461110400000

What I've tried:

    var date = String(new Date(elodata.x));

and

    var date = String(new Date(parseInt(elodata.x)));

But I get invalid date as a result.

I realise that this might not be doable because I don't know how guardian.gg handles this data. But you never know.

like image 874
Ben Spencer Avatar asked Feb 12 '17 03:02

Ben Spencer


1 Answers

You can pass in your value directly to a Date constructor in Javascript if it is an integer (which it appears to be in :

var date = new Date(elodata.x);

Likewise, you can also use the the setTime() function in Javascript to pass your integer value in if you already have an existing object :

var date = new Date();
d.setTime(elodata.x);

Example

var d1 = new Date(1461110400000);
console.log(`Constructor: ${d1}`);

var d2 = new Date();
d2.setTime(1461110400000);
console.log(`setTime(): ${d2}`);
like image 163
Rion Williams Avatar answered Oct 09 '22 23:10

Rion Williams