[英]Convert Current date in IST format using Javascript
我想使用 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()。 但是它們中的任何一個都提供了所需的格式。
試試這個它適用於 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())
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.