如何用JavaScript输出ISO 8601格式的字符串?我有一个Date对象。如何呈现title以下片段的一部分?<abbr title="2010-04-02T14:12:07">A couple days ago</abbr>我有“相对时间在文字”部分从另一个图书馆。我试过以下几点:function isoDate(msSinceEpoch) {
var d = new Date(msSinceEpoch);
return d.getUTCFullYear() + '-' + (d.getUTCMonth() + 1) + '-' + d.getUTCDate() + 'T' +
d.getUTCHours() + ':' + d.getUTCMinutes() + ':' + d.getUTCSeconds();}但这给了我:"2010-4-2T3:19"
3 回答
青春有我
TA贡献1784条经验 获得超8个赞
toISOString():
var date = new Date();date.toISOString(); //"2011-12-19T15:28:46.493Z"
if ( !Date.prototype.toISOString ) {
( function() {
function pad(number) {
var r = String(number);
if ( r.length === 1 ) {
r = '0' + r;
}
return r;
}
Date.prototype.toISOString = function() {
return this.getUTCFullYear()
+ '-' + pad( this.getUTCMonth() + 1 )
+ '-' + pad( this.getUTCDate() )
+ 'T' + pad( this.getUTCHours() )
+ ':' + pad( this.getUTCMinutes() )
+ ':' + pad( this.getUTCSeconds() )
+ '.' + String( (this.getUTCMilliseconds()/1000).toFixed(3) ).slice( 2, 5 )
+ 'Z';
};
}() );}添加回答
举报
0/150
提交
取消
