简体   繁体   中英

Convert Current date in IST format using Javascript

I want to convert current date value to the below format using javascript 2019-08-23T08:23:47+0530

I had tried the following code

      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);

and also try to convert to toUTCString(). But any of them gives the desired format.

try this it will work for ISO date

    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())

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM