Nodejs Date Format format(()

Here you can find the source of format(()

Method Source Code

Date.prototype.format = (function() {
   var/* w  w w . java  2  s  .  co m*/
      that, txts,
      roman = ['I', 'II', 'III', 'IV', 'V', 'VI', 'VII', 'VIII', 'IX', 'X', 'XI', 'XII'],
      addZero = function(num) {
         return num < 10 ? '0' + num : num;
      },
      na = function(n, z) {
         return n % z;
      },
      getDOY = function() {
         var onejan = new Date(that.getFullYear(), 0, 1);
         return Math.ceil((that - onejan) / 86400000);
      },
      getWOY = function() {
         var onejan = new Date(that.getFullYear(), 0, 1);
         return Math.ceil((((that - onejan) / 86400000) + onejan.getDay() + 1) / 7);
      },
      dateVal = function(all, found) {
         switch (found) {
            case 'DD':   return addZero(that.getDate());
            case 'D':   return that.getDate();
            case 'MM':   return addZero(that.getMonth() + 1);
            case 'M':   return that.getMonth() + 1;
            case 'R':   return roman[that.getMonth()];
            case 'YYYY': return that.getFullYear();
            case 'YY':   return that.getFullYear().toString().substr(2, 2);
            case 'hh':   return addZero(that.getHours());
            case 'h':   return that.getHours();
            case 'HH':   return addZero(na(that.getHours(), 12));
            case 'H':   return na(that.getHours(), 12);
            case 'mm':   return addZero(that.getMinutes());
            case 'm':   return that.getMinutes();
            case 'ss':   return addZero(that.getSeconds());
            case 's':   return that.getSeconds();
            case 'u':   return that.getMilliseconds();
            case 'U':   return that.getTime() / 1000;
            case 'W':   return that.getDay();
            case 'y':   return getDOY();
            case 'w':   return getWOY();
            case 'G':   return that.getTimezoneOffset();
            case 'a':   return that.getHours()>12 ? 'pm' : 'am';
            case '%x':   return txts.shift();
            default:   return '[ERROR]';
         }
      }
   ; // end var
   return function(str){
      that = this;
      txts = [].splice.call(arguments, 1);
      str = str.replace(/(DD|D|MM|M|R|YYYY|YY|hh|h|HH|H|mm|m|ss|s|u|U|W|y|w|G|a|%x)/g, dateVal);
      return str;
   };
}());

Related

  1. dateFormat(format)
    Date.prototype.dateFormat = function(format) {
        var yyyy = this.getFullYear().toString();
        format = format.replace(/yyyy/g, yyyy)
        var mm = (this.getMonth()+1).toString();
        format = format.replace(/mm/g, (mm[1]?mm:"0"+mm[0]));
        var dd  = this.getDate().toString();
        format = format.replace(/dd/g, (dd[1]?dd:"0"+dd[0]));
        var hh = this.getHours().toString();
        format = format.replace(/hh/g, (hh[1]?hh:"0"+hh[0]));
    ...
    
  2. dateFormat(value)
    function dateFormat(value) {
      if (!value)
        return new Date(0);
      var pattern = /\/Date\(([0-9]+)((-|\+)[0-9]+)?\)\
      var match = pattern.exec(value.toString());
      if (!match)
        return new Date(0);
      var utc = parseInt(match[1], 10);
      var offset = (parseInt(match[2], 10) || 0) * 10 * 60 * 60; 
    ...
    
  3. format
    ===
    
  4. format( format )
    Date.prototype.format = function( format ){
        if( typeof format !== "string" ) return this.toLocaleString();
        var match = {
            "y": this.getFullYear(),
            "M": this.getMonth() + 1,
            "d": this.getDate(),
            "h": this.getHours(),
            "m": this.getMinutes(),
            "s": this.getSeconds(),
    ...
    
  5. format( format )
    define(function(require, exports, module) {
    Date.prototype.format = function( format ){
        if( typeof format !== "string" ) return this.toLocaleString();
        var match = {
            "y": this.getFullYear(),
            "M": this.getMonth() + 1,
            "d": this.getDate(),
            "h": this.getHours(),
            "m": this.getMinutes(),
    ...
    
  6. format()
    Date.prototype.format = function () {
      'use strict';
      var
          d = this.getDate()
        , m = this.getMonth() + 1
        , y = this.getFullYear();
        return '' + y + '-' + (m<=9 ? '0' + m : m) + '-' + (d <= 9 ? '0' + d : d);
    };
    
  7. format()
    function ISODateString(aDate) {
      function pad(aNumber) {
        return aNumber < 10 ? '0' + aNumber : aNumber;
      return aDate.getUTCFullYear() + '-' +
             pad(aDate.getUTCMonth() + 1) + '-' +
             pad(aDate.getUTCDate());
    Date.prototype.format = function () {
    ...
    
  8. format()
    function ISODateString(aDate) {
      function pad(aNumber) {
        return aNumber < 10 ? '0' + aNumber : aNumber;
      return aDate.getUTCFullYear() + '-' +
             pad(aDate.getUTCMonth() + 1) + '-' +
             pad(aDate.getUTCDate());
    Date.prototype.format = function () {
    ...
    
  9. format()
    Date.prototype.format = function() {
      var d = new Date();
      var curr_date = d.getDate();
      var curr_month = d.getMonth();
      var curr_year = d.getFullYear();
      return (curr_date + "/" + curr_month + "/" + curr_year);