慕斯709654
2023-02-24 17:51:53
我想使用 javascript 2019-08-23T08:23:47+0530 將當前日期值轉換為以下格式我曾嘗試過以下代碼 var currentTime = new Date(); var currentOffset = currentTime.getTimezoneOffset(); var ISTOffset = 330; // IST offset UTC +5:30 var ISTTime = new Date(currentTime.getTime() + (ISTOffset + currentOffset)*60000);并嘗試轉換為 toUTCString()。但是它們中的任何一個都提供了所需的格式。
1 回答

LEATH
TA貢獻1936條經驗 獲得超7個贊
試試這個它適用于 ISO 日期
Date.prototype.toIsoString = function() {
var timezone = -this.getTimezoneOffset(),
DF = timezone >= 0 ? '+' : '-',
pad = function(nm) {
var narmal = Math.floor(Math.abs(nm));
return (narmal < 10 ? '0' : '') + narmal;
};
return this.getFullYear() +
'-' + pad(this.getMonth() + 1) +
'-' + pad(this.getDate()) +
'T' + pad(this.getHours()) +
':' + pad(this.getMinutes()) +
':' + pad(this.getSeconds()) +
DF + pad(timezone / 60) +
'' + pad(timezone % 60);
}
var date = new Date();
alert(date.toIsoString())
添加回答
舉報
0/150
提交
取消