Converting string time into milliseconds
Asked Answered
D

4

10

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/

Dioptase answered 3/3, 2013 at 21:34 Comment(1)
Gonna say it: NOT JSON data!Bombacaceous
Q
23

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

// ...
var k = Date.parse( d.created );
// ...
Quimper answered 3/3, 2013 at 21:40 Comment(9)
Date.parse("2013-03-01T19:54:24Z") should do as you ask. I'm fairly certain this answer is correct.Viscose
Yes, i thought so too - but in this example: jsfiddle.net/xwZjN/84 .. the text does not change - perhaps its something else?Dioptase
I finally found out what it was! I had the font styling in the incorrect order it needed to be : "Bold 11px Arial" !Dioptase
@RozzA Together with the rest of his code it worked.Quimper
this returns the unix timestamp (integer) I was hoping it returns a Date objectMyo
@RoxanaTapia Then wrap it into another new Date( Date.parse( x ) ).Quimper
@Quimper then again the milliseconds get lostMyo
Date.parse(strDate) will only work, if your strDate is in mm/dd/yyyy format.Reinertson
@Avinash This is actually not true according to MDN.Quimper
I
14

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);
Idiopathy answered 18/4, 2019 at 15:38 Comment(0)
O
8

If you string is in the form of mm:ss, Follow the procedure and resolve,

var t = "08:00";
var r = Number(t.split(':')[0])*60+Number(t.split(':')[1])*1000;
console.log(r)
Oscillatory answered 16/8, 2017 at 11:45 Comment(2)
Wrong calculation in your solution. See my solution below.Idiopathy
Hours need one more *60 and minutes also needs a *60 => var r = Number(t.split(':')[0])*60*60+Number(t.split(':')[1])*60*1000;Repellent
B
3
var timeInString = "99:99:99:99"; // Any value here
var milliseconds;
if (timeInString.split(":").length === 2) {
  /* For MM:SS */
  milliseconds =
    Number(timeInString.split(":")[0]) * 60000 +
    Number(timeInString.split(":")[1]) * 1000;
} else if (timeInString.split(":").length === 3) {
  /* For HH:MM:SS */
  milliseconds =
    Number(timeInString.split(":")[0]) * 3600000 +
    Number(timeInString.split(":")[1]) * 60000 +
    Number(timeInString.split(":")[2]) * 1000;
} else if (timeInString.split(":").length === 4) {
  /* For DD:HH:MM:SS */
  milliseconds =
    Number(timeInString.split(":")[0]) * 86400000 +
    Number(timeInString.split(":")[1]) * 3600000 +
    Number(timeInString.split(":")[2]) * 60000 +
    Number(timeInString.split(":")[3]) * 1000;
}

console.log(`Milliseconds in ${timeInString} - ${milliseconds}`);
Bolt answered 24/1, 2022 at 14:40 Comment(2)
Your answer could be improved with additional supporting information. Please edit to add further details, such as citations or documentation, so that others can confirm that your answer is correct. You can find more information on how to write good answers in the help center.Treblinka
While this code snippet may solve the problem, it doesn't explain why or how it answers the question. Please include an explanation for your code, as that really helps to improve the quality of your post. Remember that you are answering the question for readers in the future, and those people might not know the reasons for your code suggestion.Kodok

© 2022 - 2025 — McMap. All rights reserved.