2015-12-02 67 views

回答

35

簡單的方法:

console.log(now.toISOString().split('.')[0]+"Z"); 
+2

在我的書中避免使用正則表達式;) – Rob

4

使用切片除去不需要的部分

var now = new Date(); 
alert(now.toISOString().slice(0,-5)+"Z"); 
0

或可能與此覆蓋它嗎? (這是來自here的修改後的填充)

function pad(number) { 
    if (number < 10) { 
    return '0' + number; 
    } 
    return number; 
} 

Date.prototype.toISOString = function() { 
    return this.getUTCFullYear() + 
    '-' + pad(this.getUTCMonth() + 1) + 
    '-' + pad(this.getUTCDate()) + 
    'T' + pad(this.getUTCHours()) + 
    ':' + pad(this.getUTCMinutes()) + 
    ':' + pad(this.getUTCSeconds()) + 
    'Z'; 
}; 
相關問題