简体   繁体   English

字符串日期到Unix时间戳

[英]String date to unix timestamp

I m working on a scheduled TV on a raspberry pi (raspbian) in javascript/node.js 我正在javascript / node.js中的树莓派(树莓派)上的预定电视上工作

I extract the information of what to play in a .smil file, and one of the field is scheduled, wich is a string of the format "YYYY-MM-DD hh:mm:ss" 我提取了.smil文件中要播放的内容的信息,并计划了其中一个字段,该字符串的格式为“ YYYY-MM-DD hh:mm:ss”

To make comparison between them and the system time, I would like to convert it in UNIX timestamp. 为了比较它们和系统时间,我想将其转换为UNIX时间戳。

Is there a better way than a function like this one: 有没有比这样的函数更好的方法:

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

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

    ...

    return time;
}

And so on for mounth, day...? 以此类推,白天,……?

May be this can help 也许这可以帮助

var date = new Date('2009-07-15 00:00:00'.split(' ').join('T'))

that will give you date object and to get timestamp from it you can do 这将为您提供日期对象并从中获取时间戳,您可以执行

date.getTime() / 1000

dividing by 1000 because getTime will give timestamps in milliseconds 除以1000,因为getTime将提供时间戳(以毫秒为单位)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM