如何在JavaScript中将Unix时间戳转换为ISO 8601? [英] How Do I Convert a Unix Timestamp to ISO 8601 in JavaScript?
问题描述
我有一个类似 1331209044000
的时间戳,我想将其转换为ISO 8601时间戳.如何使用JavaScript进行转换?
I have a timestamp like this 1331209044000
and I want to convert it to an ISO 8601 timestamp. How can I convert it using JavaScript?
我使用jQuery的"timeago"插件- http://timeago.yarp.com/
I use the jQuery "timeago" plugin - http://timeago.yarp.com/
推荐答案
假设您的时间戳以毫秒为单位(或者您可以轻松地转换为毫秒),则可以使用 date.toISOString()
方法.
Assuming your timestamp is in milliseconds (or you can convert to milliseconds easily) then you can use the Date
constructor and the date.toISOString()
method.
var s = new Date(1331209044000).toISOString();
s; // => "2012-03-08T12:17:24.000Z"
如果您针对不支持EMCAScript 5th Edition的旧版浏览器,则可以使用此问题中列出的策略:
If you target older browsers which do not support EMCAScript 5th Edition then you can use the strategies listed in this question: How do I output an ISO 8601 formatted string in JavaScript?
这篇关于如何在JavaScript中将Unix时间戳转换为ISO 8601?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!