Nodejs Date Format format(format,date)

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

Method Source Code

Date.prototype.format = function(format,date){
    if(!format || typeof format !== 'string'){
    throw new Error('format is undefiend or type is Error');
  }//from w w  w  .j  a va 2s.  co m
  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(),
    'h+': date.getHours(),
    'm+': date.getMinutes(),
    's+': date.getSeconds()
  }
  for(reg in formatReg){
    if(new RegExp(reg).test(format)){
            var match = RegExp.lastMatch;
        format = format.replace(match, formatReg[reg].toString().slice(-match.length) );
    }
  }
  return format;
}

console.log( Date.prototype.format('yyyy-MM-dd hh:mm:ss', new Date('2015-11-11 12:15:23') ) );

Related

  1. format(format)
    Date.prototype.format = function(format) {
        var o = {
            "M+": this.getMonth() + 1,
            "d+": this.getDate(),
            "h+": this.getHours(),
            "m+": this.getMinutes(),
            "s+": this.getSeconds(),
            "q+": Math.floor((this.getMonth() + 3) / 3),
            "S": this.getMilliseconds()
    ...
    
  2. format(format)
    Date.prototype.format = function (format) {
        format = format || "yyyy-MM-dd";
        var args = {
            "M+": this.getMonth() + 1,
            "d+": this.getDate(),
            "h+": this.getHours(),
            "m+": this.getMinutes(),
            "s+": this.getSeconds(),
            "q+": Math.floor((this.getMonth() + 3) / 3),  
    ...
    
  3. 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, 
    ...
    
  4. 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();
    ...
    
  5. 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(), 
    ...
    
  6. 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(".");
    ...
    
  7. 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(".");
    ...
    
  8. 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()]);   
    ...
    
  9. 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());
    ...