我有一个类似于
我使用 jQuery 的 "timeago" 插件 - http://timeago.yarp.com/。
1331209044000
的时间戳,我想将其转换为 ISO 8601 时间戳。如何使用 JavaScript 进行转换?我使用 jQuery 的 "timeago" 插件 - http://timeago.yarp.com/。
1331209044000
的时间戳,我想将其转换为 ISO 8601 时间戳。如何使用 JavaScript 进行转换?Date
构造函数和date.toISOString()
方法。var s = new Date(1331209044000).toISOString();
s; // => "2012-03-08T12:17:24.000Z"
new Date(timestamp * 1000).toISOString();
。 - Josef Kufner我使用了提供的链接,解决了问题。
// convert to ISO 8601 timestamp
function ISODateString(d){
function pad(n){return n<10 ? '0'+n : n}
return d.getUTCFullYear()+'-'
+ pad(d.getUTCMonth()+1)+'-'
+ pad(d.getUTCDate())+'T'
+ pad(d.getUTCHours())+':'
+ pad(d.getUTCMinutes())+':'
+ pad(d.getUTCSeconds())+'Z'
}
var d = new Date(parseInt(date));
console.log(ISODateString(d));