javascript 将字符串时间转换为毫秒

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/15191184/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-26 23:48:36  来源:igfitidea点击:

Converting string time into milliseconds

javascriptdatetimed3.jstimestampmilliseconds

提问by Jose

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.

我有一组单独的 json 数据,它们每个都有一个时间戳,用于以这种确切的格式创建,例如

 [ {"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:

因此,我希望在一个函数中使用“created”,该函数计算如果数据是从今天起 60 天或更短时间输入的,它将以斜体显​​示。但是,我尝试的功能没有效果。我试图以毫秒为单位进行计算:

     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/

编辑:示例 - http://jsfiddle.net/xwZjN/84/

回答by Sirko

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

要获取像您这样的日期字符串的纪元以来的毫秒数,请使用Date.parse()

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

回答by KARTHIKEYAN.A

Follow the procedure and resolve,

按照程序解决,

var t = "08:00";
var r = Number(t.split(':')[0])*60+Number(t.split(':')[1])*1000;
console.log(r)

回答by Ank

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