且构网

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

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

更新时间:2023-01-22 12:23:58

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

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