Nodejs Date Format format(format)

Here you can find the source of format(format)

Method Source Code

/*exported nvl, fixLen, getUserAgent */

/*/*  w  w w . ja  va2 s  .c  o m*/
 * Some date functions
 */
Date.prototype.format = function(format) // author: meizz
{
  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, // month
    "d+" : this.getDate(), // day
    "h+" : this.getHours(), // hour
    "i+" : this.getHours() + 1, // hour + 1
    "m+" : this.getMinutes(), // minute
    "s+" : this.getSeconds(), // second
    "q+" : Math.floor((this.getMonth() + 3) / 3), // quarter
    "S" : this.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));
    }
  }
  if (/(N+)/.test(format)) {
    format = format.replace(RegExp.$1, monName[this.getMonth()]);
  }
  if (/(W+)/.test(format)) {
    format = format.replace(RegExp.$1, dayName[this.getDay()]);
  }
  return format;
};

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), 
    ...
    
  2. 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()
    ...
    
  3. format(format)
    Date.prototype.format = function(format) {
      if (format === undefined)
        return this.toString();
      var formatSeparator = format.match(/[.\/\-\s].*?/);
      var formatParts     = format.split(/\W+/);
      var result          = '';
      for (var i=0; i<=formatParts.length; i++) {
        switch(formatParts[i]) {
          case 'd':
    ...
    
  4. 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()
    ...
    
  5. 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),  
    ...
    
  6. 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();
    ...
    
  7. 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(), 
    ...
    
  8. 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(),
    ...
    
  9. 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(".");
    ...