如何在javascript中将字符串转换为Unix时间戳? [英] how to convert a string to a Unix timestamp in javascript?

查看:169
本文介绍了如何在javascript中将字符串转换为Unix时间戳?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想将字符串2013-09-05 15:34:00转换为javascript中的Unix时间戳。谁能告诉你怎么做?谢谢。

I want to convert a string "2013-09-05 15:34:00" into a Unix timestamp in javascript. Can any one tell how to do that? thanks.

推荐答案

您可以初始化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)

这篇关于如何在javascript中将字符串转换为Unix时间戳?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆