Converting string time into milliseconds

Jose picture Jose · Mar 3, 2013 · Viewed 37k times · Source

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/

Answer

Sirko picture Sirko · Mar 3, 2013

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

// ...
var k = Date.parse( d.created );
// ...