Nodejs Date ISO Format toISO8601String(format, offset)

Here you can find the source of toISO8601String(format, offset)

Method Source Code

Date.prototype.toISO8601String = function (format, offset) {
    /* accepted values for the format [1-6]:
     1 Year://  w  w w. java2  s.  c  o m
       YYYY (eg 1997)
     2 Year and month:
       YYYY-MM (eg 1997-07)
     3 Complete date:
       YYYY-MM-DD (eg 1997-07-16)
     4 Complete date plus hours and minutes:
       YYYY-MM-DDThh:mmTZD (eg 1997-07-16T19:20+01:00)
     5 Complete date plus hours, minutes and seconds:
       YYYY-MM-DDThh:mm:ssTZD (eg 1997-07-16T19:20:30+01:00)
     6 Complete date plus hours, minutes, seconds and a decimal
       fraction of a second
       YYYY-MM-DDThh:mm:ss.sTZD (eg 1997-07-16T19:20:30.45+01:00)
    */
    if (!format) { var format = 6; }
    if (!offset) {
        var offset = 'Z';
        var date = this;
    } else {
        var d = offset.match(/([-+])([0-9]{2}):([0-9]{2})/);
        var offsetnum = (Number(d[2]) * 60) + Number(d[3]);
        offsetnum *= ((d[1] == '-') ? -1 : 1);
        var date = new Date(Number(Number(this) + (offsetnum * 60000)));
    }

    var zeropad = function (num) { return ((num < 10) ? '0' : '') + num; }

    var str = "";
    str += date.getUTCFullYear();
    if (format > 1) { str += "-" + zeropad(date.getUTCMonth() + 1); }
    if (format > 2) { str += "-" + zeropad(date.getUTCDate()); }
    if (format > 3) {
        str += "T" + zeropad(date.getUTCHours()) +
               ":" + zeropad(date.getUTCMinutes());
    }
    if (format > 5) {
        var secs = Number(date.getUTCSeconds() + "." +
                   ((date.getUTCMilliseconds() < 100) ? '0' : '') +
                   zeropad(date.getUTCMilliseconds()));
        str += ":" + zeropad(secs);
    } else if (format > 4) { str += ":" + zeropad(date.getUTCSeconds()); }

    if (format > 3) { str += offset; }
    return str;
}

Related

  1. isoDate()
    Date.prototype.isoDate = function(){
      var d = this;
      return d.year();
    function toJSArray(arr) {
      var len = [arr count],
          res = [];
      while(len--){
        res.push(arr[len]);
    ...
    
  2. rfc3339()
    function f(n) {
        return n < 10 ? '0' + n : n;
    Date.prototype.rfc3339 = function() {
        return this.getUTCFullYear()   + '-' +
             f(this.getUTCMonth() + 1) + '-' +
             f(this.getUTCDate())      + 'T' +
             f(this.getUTCHours())     + ':' +
             f(this.getUTCMinutes())   + ':' +
    ...
    
  3. rfc3339()
    function f(n) { 
        return n < 10 ? '0' + n : n;
    Date.prototype.rfc3339 = function() {
        return this.getUTCFullYear() + '-' +
             f(this.getUTCMonth() + 1) + '-' +
             f(this.getUTCDate()) + 'T' +
             f(this.getUTCHours()) + ':' +
             f(this.getUTCMinutes()) + ':' +
    ...
    
  4. rfc3339()
    function f(n) {    
        return n < 10 ? '0' + n : n;
    Date.prototype.rfc3339 = function() {
        return this.getUTCFullYear()   + '-' +
             f(this.getUTCMonth() + 1) + '-' +
             f(this.getUTCDate())      + 'T' +
             f(this.getUTCHours())     + ':' +
             f(this.getUTCMinutes())   + ':' +
    ...
    
  5. rfc822date()
    "use strict";
    Date.prototype.rfc822date = function() {
        var weekday_name = { 0:'Sun', 1:'Mon', 2:'Tue', 3:'Wed', 4:'Thu', 5:'Fri', 6: 'Sat' };
        var month_name = { 0:'Jan', 1:'Feb', 2:'Mar', 3:'Apr', 4:'May', 5:'Jun', 6: 'Jul', 7:'Aug', 8:'Sep', 9:'Oct', 10:'Nov', 11:'Dec' };
        var zeropad = function(num, length) {
            if ( length === undefined ) {
                length = 2;
            var add = length - num.toString().length;
    ...
    
  6. toISO8601String(offset)
    Date.prototype.toISO8601String = function (offset) {
        if (!offset) {
            var offset = 'Z';
            var date = this;
        } else {
            var d = offset.match(/([-+])([0-9]{2}):([0-9]{2})/);
            var offsetnum = (Number(d[2]) * 60) + Number(d[3]);
            offsetnum *= ((d[1] == '-') ? -1 : 1);
            var date = new Date(Number(Number(this) + (offsetnum * 60000)));
    ...
    
  7. toISODateString()
    Date.prototype.toISODateString = function() {
        function pad(n){
            return n < 10 ? '0' + n : n
        function pad3(n){
            if (n < 10) return '00' + n;
            if (n < 100) return '0' + n;
            return n;
        return this.getUTCFullYear()+'-'
        + pad(this.getUTCMonth()+1)+'-'
        + pad(this.getUTCDate())+'T'
        + pad(this.getUTCHours())+':'
        + pad(this.getUTCMinutes())+':'
        + pad(this.getUTCSeconds())+'.'
        + pad3(this.getUTCMilliseconds())+'Z'
    
  8. toISOLocalString()
    Date.prototype.toISOLocalString = function() {
        if ( this.toString() === 'Invalid Date' ) {
            return this.toString();
        return new Date( this.getTime() - ( this.getTimezoneOffset() * 60 * 1000 ) ).toISOString()
            .replace( 'Z', this.getTimezoneOffsetAsTime() );
    };
    Date.prototype.getTimezoneOffsetAsTime = function() {
        var offsetMinutesTotal;
    ...
    
  9. toISOLocalString()
    Date.prototype.toISOLocalString = function() {
        var offset = {},
            pad2 = function( x ) {
                return ( x < 10 ) ? '0' + x : x;
            };
        if ( this.toString() === 'Invalid Date' ) {
            return this.toString();
        offset.minstotal = this.getTimezoneOffset();
    ...