Nodejs Date ISO Format toISOLocalString()

Here you can find the source of toISOLocalString()

Method Source Code

/**//from   w  w w. ja  v a  2s .com
 * Converts a native Date UTC String to a RFC 3339-compliant date string with local offsets
 * used in JavaRosa, so it replaces the Z in the ISOstring with a local offset
 * @return {string} a datetime string formatted according to RC3339 with local offset
 */
Date.prototype.toISOLocalString = function() {
    //2012-09-05T12:57:00.000-04:00 (ODK)
    var offset = {},
        pad2 = function( x ) {
            return ( x < 10 ) ? '0' + x : x;
        };

    if ( this.toString() === 'Invalid Date' ) {
        return this.toString();
    }

    offset.minstotal = this.getTimezoneOffset();
    offset.direction = ( offset.minstotal < 0 ) ? '+' : '-';
    offset.hrspart = pad2( Math.abs( Math.floor( offset.minstotal / 60 ) ) );
    offset.minspart = pad2( Math.abs( Math.floor( offset.minstotal % 60 ) ) );

    return new Date( this.getTime() - ( offset.minstotal * 60 * 1000 ) ).toISOString()
        .replace( 'Z', offset.direction + offset.hrspart + ':' + offset.minspart );
};

Related

  1. 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;
    ...
    
  2. toISO8601String(format, offset)
    Date.prototype.toISO8601String = function (format, offset) {
        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);
    ...
    
  3. 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)));
    ...
    
  4. 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'
    
  5. 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;
    ...
    
  6. toISOString()
    Date.prototype.toISOString = function() {
      return this.getUTCFullYear()
        + "-" + ("0" + (this.getUTCMonth() + 1)).substr(-2, 2)
        + "-" + ("0" + this.getUTCDate()).substr(-2, 2)
        + "T" + ("0" + this.getUTCHours()).substr(-2, 2)
        + ":" + ("0" + this.getUTCMinutes()).substr(-2, 2)
        + ":" + ("0" + this.getUTCSeconds()).substr(-2, 2)
        + "Z";
    
  7. toISOString()
    Date.prototype.toISOString = function() {
      return this.getFullYear() + '-' + zpad(this.getMonth() + 1) + '-' + zpad(this.getDate()) + 'T' + zpad(this.getHours()) + ':' + zpad(this.getMinutes());
    
  8. toISOString()
    function OzeroPad(val) {
      return val < 10 ? '0' + val : '' + val;
    if(!Date.prototype.toISOString) {
      Date.prototype.toISOString = function () {
        var YYYY = this.getUTCFullYear(),
        MM = OzeroPad(this.getUTCMonth() + 1),
        DD = OzeroPad(this.getUTCDate()),
        HH = OzeroPad(this.getUTCHours()),
    ...
    
  9. toISOString()
    Date.prototype.toISOString = Date.prototype.toISOString || function() {
      return this.getUTCFullYear() + "-"
        + ("0" + this.getUTCMonth() + 1 + "-").slice(-3)
        + ("0" + this.getUTCDate() + "T").slice(-3)
        + ("0" + this.getUTCHours() + ":").slice(-3)
        + ("0" + this.getUTCMinutes() + ":").slice(-3)
        + ("0" + this.getUTCSeconds() + ".").slice(-3)
        + ("00" + this.getUTCMilliseconds() + "Z").slice(-4);
    };
    ...