[英]Javascript 'yyyy-mm-dd hh:mm:ss' to timestamp
I want to convert datetime like '2015-05-01 05:13:43' into timestamp. 我想将日期时间如'2015-05-01 05:13:43'转换为时间戳。 Is there Any way to do it using JavaScript?
有没有办法使用JavaScript?
With pure JS you can try with: 使用纯JS,您可以尝试:
new Date(Date.parse('2015-05-01 05:13:43+0000')).getTime() / 1000
It's important to add +0000
at the end of the string - otherwise browser will use your local timezone and add/remove few hours from the result. 在字符串末尾添加
+0000
非常重要 - 否则浏览器将使用您的本地时区并在结果中添加/删除几个小时。
getTime
method gives you time in ms - so we have to divide it by 1000
. getTime
方法给你时间毫秒 - 所以我们必须将它除以1000
。
You can do it! 你能行的!
let dateToConvert = '2015-05-01 05:13:43'
let date = new Date(dateToConvert)
let timestamp = date.getTime()
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.