Skip to content Skip to sidebar Skip to footer

Javascript Date.prototype.toisostring() Loses Offset

Why does this method use UTC timezone (Z) and not include the local time offset (+/-HH:SS) instead? The 'ISO' in the method name refers to ISO 8601—which allows for 'time zone de

Solution 1:

This is one of those "because that's what the language specification says" answers (see ECMA-262 §20.3.4.36). ISO 8601 is a format, and while it allows the use of timezone data, ECMAScript only uses UTC. You can extend Date.prototype with your own toLocalISOString method if you wish. BTW, writing such a method is not difficult.

// Format date as ISO 8601 long format with local timezone offsetif (!Date.prototype.toLocalISOString) {
  Date.prototype.toLocalISOString = function() {
  
  // Helper for paddingfunctionpad(n, len) {
    return ('000' + n).slice(-len);
  }

  // If not called on a Date instance, or timevalue is NaN, return undefinedif (isNaN(this) || Object.prototype.toString.call(this) != '[object Date]') return;

  // Otherwise, return an ISO format string with the current system timezone offsetvar d = this;
  var os = d.getTimezoneOffset();
  var sign = (os > 0? '-' : '+');
  os = Math.abs(os);

  returnpad(d.getFullYear(), 4) + '-' +
         pad(d.getMonth() + 1, 2) + '-' +
         pad(d.getDate(), 2) +
         'T' + 
         pad(d.getHours(), 2) + ':' +
         pad(d.getMinutes(), 2) + ':' +
         pad(d.getSeconds(), 2) + '.' +
         pad(d.getMilliseconds(), 3) + 
       
         // Note sign of ECMASCript offsets are opposite to ISO 8601
         sign +
         pad(os/60 | 0, 2) + ':' +
         pad(os%60, 2);
  }
}
document.write(newDate().toLocalISOString())

Edit

Based on a post by DanDascalescu, here's an alternative that might be more efficient as it has fewer function calls, but it creates two additional Date objects:

// Return a string in ISO 8601 extended format with the host timezone offsetDate.prototype.toLocalISOString = function() {

    // If not called on a Date instance, or timevalue is NaN, return undefinedif (isNaN(this) || Object.prototype.toString.call(this) != '[object Date]') return;

    // Copy date so don't modify originalvar d = newDate(+this);
    var offset = d.getTimezoneOffset();
    var offSign = offset > 0? '-' : '+';
    offset = Math.abs(offset);
    var tz = offSign + ('0' + (offset/60|0)).slice(-2) + ':' + ('0' + offset%60).slice(-2)
    returnnewDate(d.setMinutes(d.getMinutes() - d.getTimezoneOffset())).toISOString().slice(0,-1) + tz; 
}

console.log(newDate().toLocalISOString())

Post a Comment for "Javascript Date.prototype.toisostring() Loses Offset"