如何在JavaScript中将Unix时间戳转换为ISO 8601? [英] How Do I Convert a Unix Timestamp to ISO 8601 in JavaScript?

查看:86
本文介绍了如何在JavaScript中将Unix时间戳转换为ISO 8601?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个类似 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/

推荐答案

假设您的时间戳以毫秒为单位(或者您可以轻松地转换为毫秒),则可以使用

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屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆