Nodejs Date Format format(format,value)

Here you can find the source of format(format,value)

Method Source Code

Date.prototype.format = function (format,value) {
   /*// w w  w . ja v  a  2 s. c o m
    * eg:format="yyyy-MM-dd hh:mm:ss";
    */
   if (!format) {
      format = "yyyy-MM-dd hh:mm:ss";
   }
   if(value==''||value==null){
      return '';
   }
   var strdata=value.replace(/-/g,"/");
   var index=strdata.indexOf(".");
   if(index>0)
   {
      strdata=strdata.substr(0,index);
   }
   var date= new Date(Date.parse(strdata));
   var o = {
      "M+" : date.getMonth() + 1, // month
      "d+" : date.getDate(), // day
      "h+" : date.getHours(), // hour
      "m+" : date.getMinutes(), // minute
      "s+" : date.getSeconds(), // second
      "q+" : Math.floor((date.getMonth() + 3) / 3), // quarter
      "S" : date.getMilliseconds()
      // millisecond
   };
   
   if (/(y+)/.test(format)) {
      format = format.replace(RegExp.$1, (this.getFullYear() + "").substr(4 - RegExp.$1.length));
   }
   
   for (var k in o) {
      if (new RegExp("(" + k + ")").test(format)) {
         format = format.replace(RegExp.$1, RegExp.$1.length == 1 ? o[k] : ("00" + o[k]).substr(("" + o[k]).length));
      }
   }
   return format;
};

Related

  1. format(format)
    Date.prototype.format = function(format) 
      var monName = [ "Jan", "Feb", "Mar", "Apr", "May", "Jun", "Jul", "Aug", "Sep", "Oct", "Nov", "Dec" ];
      var dayName = [ "Sun", "Mon", "Tue", "Wed", "Thu", "Fri", "Sat" ];
      var o = {
        "M+" : this.getMonth() + 1, 
        "d+" : this.getDate(), 
        "h+" : this.getHours(), 
        "i+" : this.getHours() + 1, 
    ...
    
  2. format(format)
    Date.prototype.format = function(format) {
      var self = this;
      var h = self.getHours();
      var m = self.getMinutes().toString();
      var s = self.getSeconds().toString();
      var M = (self.getMonth() + 1).toString();
      var yyyy = self.getFullYear().toString();
      var yy = self.getYear().toString();
      var d = self.getDate().toString();
    ...
    
  3. format(format)
    var fs = require('fs');
    var readline = require('readline');
    Date.prototype.format = function(format) {
      var o = {
        "M+" : this.getMonth()+1, 
        "d+" : this.getDate(),    
        "h+" : this.getHours(),   
        "m+" : this.getMinutes(), 
        "s+" : this.getSeconds(), 
    ...
    
  4. format(format,date)
    Date.prototype.format = function(format,date){
        if(!format || typeof format !== 'string'){
        throw new Error('format is undefiend or type is Error');
      date = date instanceof Date? date : (typeof date === 'number'|| typeof date === 'string')? new Date(date): new Date();
      var formatReg = {
        'y+': date.getFullYear(),
        'M+': date.getMonth()+1,
        'd+': date.getDate(),
    ...
    
  5. 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(".");
    ...
    
  6. 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()]);   
    ...
    
  7. 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());
    ...
    
  8. 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();
    ...
    
  9. 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(),
    ...