将Unix时间戳转换为ISO 8601

将Unix时间戳转换为ISO 8601

问题描述:

我想使用jquery timeago插件 - http://timeago.yarp.com/

I want to use the jquery timeago plugin - http://timeago.yarp.com/

我有这样的时间戳 1331209044000 ,文档说我需要ISO 8601时间戳。

I have timestamps like this 1331209044000 and the docs say i need an ISO 8601 timestamp.

说实话,我从未听说过ISO 8601。

To be honest i have never heard of ISO 8601.

我如何转换它?

干杯

假设您的时间戳以毫秒为单位(或者您可以轻松转换为毫秒),那么您可以使用 日期构造函数 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第5版中,您可以使用此问题中列出的策略:如何在JavaScript中输出ISO 8601格式的字符串?

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?