3 回答

TA貢獻(xiàn)1853條經(jīng)驗(yàn) 獲得超6個(gè)贊
moment沒有直接的方案,我自己貼個(gè)方案好了。
Date.prototype.toIsoString = function() {
var tzo = -this.getTimezoneOffset(),
dif = tzo >= 0 ? '+' : '-',
pad = function(num) {
var norm = Math.floor(Math.abs(num));
return (norm < 10 ? '0' : '') + norm;
},
padMilli = function(num) {
var norm = Math.floor(Math.abs(num));
if (norm >= 10 && norm < 100) {
return '0' + norm;
}
if (norm < 10) {
return '00' + norm;
}
return norm;
};
return this.getFullYear() +
'-' + pad(this.getMonth() + 1) +
'-' + pad(this.getDate()) +
'T' + pad(this.getHours()) +
':' + pad(this.getMinutes()) +
':' + pad(this.getSeconds()) +
'.' + padMilli(this.getMilliseconds()) +
dif + pad(tzo / 60) + pad(tzo % 60);
}
var dt = new Date();
console.log(dt.toIsoString());
添加回答
舉報(bào)