且构网

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

如何在javascript中将字符串转换为Unix时间戳?

更新时间:2021-07-25 05:31:46

您可以初始化Date对象并调用getTime()以unix形式获取它。它以毫秒为单位,因此您需要除以1000才能在几秒钟内得到它。

You can initialise a Date object and call getTime() to get it in unix form. It comes out in milliseconds so you'll need to divide by 1000 to get it in seconds.

(new Date("2013/09/05 15:34:00").getTime()/1000)

它可能有小数位,所以将它包装在Math.round中就可以清除它。

It may have decimal bits so wrapping it in Math.round would clean that.

Math.round(new Date("2013/09/05 15:34:00").getTime()/1000)