简体   繁体   English

Javascript'yyyy-mm-dd hh:mm:ss'到时间戳

[英]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.

相关问题 如何使用 Javascript 或 jQuery 从 YYYY-MM-DD HH:MM:SS 修剪 HH:MM:SS - How to trim HH:MM:SS from YYYY-MM-DD HH:MM:SS using Javascript or jQuery 如何使用JavaScript将数据格式“ YYYY-mm-dd hh:mm:ss”转换为“ dd-mm-YYYY hh:mm:ss”? - How to convert data format “YYYY-mm-dd hh:mm:ss” to “dd-mm-YYYY hh:mm:ss” using javascript? Ext.js-将时间戳记格式设置为yyyy-mm-dd hh:mm:ss [.fffffffff] - Ext.js - Format TimeStamp into yyyy-mm-dd hh:mm:ss[.fffffffff] 如何将“YYYY-MM-DD hh:mm:ss”格式的日期转换为 UNIX 时间戳 - How to convert date in format "YYYY-MM-DD hh:mm:ss" to UNIX timestamp 从yyyy-mm-dd获取当前时间戳hh:mm:ss使用UTC / GMT + 0作为纪元 - Get current timestamp from yyyy-mm-dd hh:mm:ss using UTC/GMT+0 as epoch Javascript将yyyy-mm-dd hh:mm:ss转换为newDate(“ day,month date year hh:mm:ss”) - Javascript convert yyyy-mm-dd hh:mm:ss like newDate(“day, month date year hh:mm:ss”) 将毫秒转换为 yyyy-MM-dd HH:mm:ss 格式 Javascript - Convert Milliseconds to yyyy-MM-dd HH:mm:ss format Javascript 如何检查日期时间是否在范围之间? jQuery或javascript中的YYYY-MM-DD HH:MM:SS格式 - How to check datetime if it falls between a range? YYYY-MM-DD HH:MM:SS format in jquery or javascript yyyy-mm-dd hh:mm:ss的javascript转换帮助 - javascript conversion of yyyy-mm-dd hh:mm:ss aa help 如何在 javascript 中解析 yyyy-MM-dd HH:mm:ss.SSS 格式的日期? - How to parse yyyy-MM-dd HH:mm:ss.SSS format date in javascript?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM