Javascript 字符串日期到unix时间戳

Javascript 字符串日期到unix时间戳,javascript,node.js,date,timestamp,Javascript,Node.js,Date,Timestamp,我正在javascript/node.js中的raspberry-pi-raspbian上制作一个定时电视 我在一个.smil文件中提取要播放的内容的信息,其中一个字段被调度,它是格式为YYYY-MM-DD hh:MM:ss的字符串 为了将它们与系统时间进行比较,我想将其转换为UNIX时间戳 有没有比这样的函数更好的方法: function (stime){ var time=0, cache=0, scache; scache=stime.substr(0, 4);

我正在javascript/node.js中的raspberry-pi-raspbian上制作一个定时电视

我在一个.smil文件中提取要播放的内容的信息,其中一个字段被调度,它是格式为YYYY-MM-DD hh:MM:ss的字符串

为了将它们与系统时间进行比较,我想将其转换为UNIX时间戳

有没有比这样的函数更好的方法:

function (stime){
    var time=0, cache=0, scache;

    scache=stime.substr(0, 4);
    cache=parseInt(scache);
    time=cache*365*24*60*60;

    ...

    return time;
}
等等,对于mounth,day…?

这可能会有所帮助

var date = new Date('2009-07-15 00:00:00'.split(' ').join('T'))
这将为您提供date对象并从中获取时间戳

date.getTime() / 1000

除以1000,因为getTime将以毫秒为单位给出时间戳

我认为它不再以毫秒为单位返回日期:2016-05-10T19:27:21.338Z方法。getTime/1000提供1462908441.338。对于这种情况,我建议使用以下代码片段:~~date.getTime/1000