Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Converting string time into milliseconds

I have a set of individual json data, they each have a time stamp for when it was created in this exact format e.g.

 [ {"Name": "Jake", "created":"2013-03-01T19:54:24Z" },
   {"Name": "Rock", "created":"2012-03-01T19:54:24Z" } ]

As a result I wish to use 'created' within a function which calculates that if the data was entered 60days or less from today it will appear in italic. However, the function I've attempted has no effect. I'm attempting to do the calculation in milliseconds:

     node.append("text")
    .text(function(d) { return d.Name; })
    .style("font", function (d)
           { var date = new Date(d.created);
             var k = date.getMilliseconds;
             var time = new Date ();
             var n = time.getTime();

       if(k > (n - 5184000) )  {return " Arial 11px italic"; }
                     else { return " Arial 11px " ; }


        })

I am curious whether I am actually converting the data at all into milliseconds. Also, if I am getting todays date in milliseconds.

Thanks in advance

EDIT: Example - http://jsfiddle.net/xwZjN/84/

like image 490
Jose Avatar asked Mar 03 '13 21:03

Jose


2 Answers

To get the milliseconds since epoch for a date-string like yours, use Date.parse():

// ...
var k = Date.parse( d.created );
// ...
like image 85
Sirko Avatar answered Sep 30 '22 09:09

Sirko


let t = "08:30"; // hh:mm
let ms = Number(t.split(':')[0]) * 60 * 60 * 1000 + Number(t.split(':')[1]) * 60 * 1000;
console.log(ms);

let t = "08:30"; // mm:ss
let ms = Number(t.split(':')[0]) * 60 * 1000 + Number(t.split(':')[1]) * 1000;
console.log(ms);
like image 33
Ank Avatar answered Sep 30 '22 10:09

Ank