且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

将日期和时间转换为 Unix 时间戳

更新时间:2023-01-22 12:24:16

如果你只需要一个好的日期解析功能,我会看看 date.js.它将接受您可以抛出的几乎任何日期字符串,并返回一个 JavaScript 日期对象.

If you just need a good date-parsing function, I would look at date.js. It will take just about any date string you can throw at it, and return you a JavaScript Date object.

一旦你有了一个 Date 对象,你就可以调用它的 getTime() 方法,它会给你自 1970 年 1 月 1 日以来的毫秒数.只需将该结果除以 1000 即可获得 unix 时间戳值.

Once you have a Date object, you can call its getTime() method, which will give you milliseconds since January 1, 1970. Just divide that result by 1000 to get the unix timestamp value.

在代码中,只包含 date.js,然后:

In code, just include date.js, then:

var unixtime = Date.parse("24-Nov-2009 17:57:35").getTime()/1000