Nodejs Date Format format(pattern)

Here you can find the source of format(pattern)

Method Source Code

Date.prototype.format = function(pattern)
{
   if(!pattern){// w  w  w  . jav  a 2s.c  o m
      pattern = "yyyy-MM-dd";
   }
   var o =
   {
      "M+" : this.getMonth() + 1, // 
      "d+" : this.getDate(), // 
      "h+" : this.getHours() % 12 == 0 ? 12 : this.getHours() % 12, // 
      "H+" : this.getHours(), // 
      "m+" : this.getMinutes(), // 
      "s+" : this.getSeconds(), //
      "q+" : Math.floor((this.getMonth() + 3) / 3), //
      "S" : this.getMilliseconds()
   };
   var week =
   {
      "0" : "\u65e5",
      "1" : "\u4e00",
      "2" : "\u4e8c",
      "3" : "\u4e09",
      "4" : "\u56db",
      "5" : "\u4e94",
      "6" : "\u516d"
   };
   if (/(y+)/.test(pattern))
   {
      pattern = pattern.replace(RegExp.$1, (this.getFullYear() + "")
            .substr(4 - RegExp.$1.length));
   }
   if (/(E+)/.test(pattern))
   {
      pattern = pattern
            .replace(
                  RegExp.$1,
                  ((RegExp.$1.length > 1) ? (RegExp.$1.length > 2 ? "\u661f\u671f"
                        : "\u5468")
                        : "")
                        + week[this.getDay() + ""]);
   }
   for ( var k in o)
   {
      if (new RegExp("(" + k + ")").test(pattern))
      {
         pattern = pattern.replace(RegExp.$1, (RegExp.$1.length == 1) ? (o[k])
               : (("00" + o[k]).substr(("" + o[k]).length)));
      }
   }
   return pattern;
};

Related

  1. format(format,value)
    Date.prototype.format = function (format,value) {
      if (!format) {
        format = "yyyy-MM-dd hh:mm:ss";
      if(value==''||value==null){
        return '';
      var strdata=value.replace(/-/g,"/");
      var index=strdata.indexOf(".");
    ...
    
  2. format(formatStr)
    Date.prototype.format = function(formatStr)   
        var str = formatStr;   
        var Week = ['?','?','?','?','?','?','?'];  
        str=str.replace(/yyyy|YYYY/,this.getFullYear());   
        str=str.replace(/yy|YY/,(this.getYear() % 100)>9?(this.getYear() % 100).toString():'0' + (this.getYear() % 100));   
        str=str.replace(/MM/,this.getMonth()>9?this.getMonth().toString():'0' + this.getMonth());   
        str=str.replace(/M/g,this.getMonth());   
        str=str.replace(/w|W/g,Week[this.getDay()]);   
    ...
    
  3. format(formatStr)
    Date.prototype.format = function(formatStr) {
        var str = formatStr;
        var Week = ['?','?','?','?','?','?','?'];
        str = str.replace(/yyyy|YYYY/,this.getFullYear());
        str = str.replace(/yy|YY/,(this.getYear() % 100)>9?(this.getYear() % 100).toString():'0' + (this.getYear() % 100));
        str = str.replace(/MM/,this.getMonth()>9?this.getMonth().toString():'0' + this.getMonth());
        str = str.replace(/M/g,this.getMonth());
        str = str.replace(/w|W/g,Week[this.getDay()]);
        str = str.replace(/dd|DD/,this.getDate()>9?this.getDate().toString():'0' + this.getDate());
    ...
    
  4. format(formatStr)
    Date.prototype.format= function(formatStr) 
    var date = this;
    var timeValues = function(){};
    timeValues.prototype = {
       year:function(){
        if(formatStr.indexOf("yyyy")>=0)
         return date.getYear();
    ...
    
  5. format(pattern)
    Date.prototype.format = function(pattern) {
      var returnValue = pattern;
      var format = {
        "y+": this.getFullYear(),
        "M+": this.getMonth() + 1,
        "d+": this.getDate(),
        "H+": this.getHours(),
        "m+": this.getMinutes(),
        "s+": this.getSeconds(),
    ...
    
  6. format(pattern)
    Date.prototype.format = function(pattern){
        var pattern = pattern;    
        var dateObj = {
            "Y" : this.getFullYear(),
            "M" : this.getMonth()+1,
            "D" : this.getDate(),
            "h" : this.getHours(),
            "m" : this.getMinutes(),
            "s" : this.getSeconds()
    ...
    
  7. format(pattern, gmt)
    Date.prototype.format = function(pattern, gmt) {
      var result = '';
      var date = this;
      if(gmt != undefined) {
        var utc = date.getTime() + (date.getTimezoneOffset() * 60000);
        date = new Date(utc + (3600000 * gmt));
      for (var i = 0; i < pattern.length; i++) {
        var c = pattern[i];
    ...
    
  8. format(str)
    Date.prototype.format = function (str) {
      const o = {
        'y+': this.getFullYear(),
        'M+': this.getMonth() + 1,
        'd+': this.getDate(),
        'h+': this.getHours(),
        'm+': this.getMinutes(),
        's+': this.getSeconds()
      Object.keys(o).forEach(element => {
        str = str.replace(new RegExp(element), substr => {
          return String.prototype.padStart.call(o[element], substr.length, '0')
        })
      })
      return str
    
  9. format(strFormat)
    Date.prototype.format = function (strFormat) {
      'use strict';
      var d = new Date(this),
        mon = ['', 'Jan', 'Feb', 'Mar', 'Apr', 'May', 'Jun', 'Jul', 'Aug', 'Sep', 'Oct', 'Nov', 'Dec'],
        month = ['', 'January', 'February', 'March', 'April', 'May', 'June', 'July', 'August', 'September', 'October', 'November', 'December'],
        day = ['Sun', 'Mon', 'Tue', 'Wed', 'Thu', 'Fri', 'Sat'],
        weekday = ['Sunday', 'Monday', 'Tuesday', 'Wednesday', 'Thursday', 'Friday', 'Saturday'],
        v = {
          's': String(d.getSeconds()),
    ...