Javascript将人类时间转换为时间戳

编程入门 行业动态 更新时间:2024-10-22 13:42:22
本文介绍了Javascript将人类时间转换为时间戳的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述

使用javascript,如何将人造时间字符串(例如Wed Jun 20 19:20:44 +0000 2012)转换为时间戳记值,如1338821992?

Using javascript, How can I convert a "human time" string like "Wed Jun 20 19:20:44 +0000 2012" into a timestamp value like "1338821992"?

推荐答案

只需创建一个 Date 对象,并执行 .getTime() 或使用 Date.parse() :

Just create a Date object from it and do .getTime() or use Date.parse():

var d = new Date("Wed Jun 20 19:20:44 +0000 2012"); d.getTime(); //returns 1340220044000 //OR Date.parse("Wed Jun 20 19:20:44 +0000 2012"); //returns 1340220044000

如果您的人造时间字符串的格式为Date构造函数了解(您发布的示例)。

Works great if your "human time" string is in a format that the Date constructor understands (which the example you posted is).

编辑

实现你可能意味着一个Unix时间戳,它是从时代开始的秒数(不像ms时间戳的ms)。在这种情况下,只需将JS时间戳划分为 1000 :

Realized you may mean a Unix timestamp, which is seconds passed since the epoch (not ms like JS timestamps). In that case simply divide the JS timestamp by 1000:

//if you want to truncate ms instead of rounding just use Math.floor() Math.round(Date.parse("Wed Jun 20 19:20:44 +0000 2012") / 1000); //returns 1340220044

更多推荐

Javascript将人类时间转换为时间戳

本文发布于:2023-10-27 15:53:33,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1533842.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:时间   转换为   人类   Javascript

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!