Nodejs Date Format format(format)

Here you can find the source of format(format)

Method Source Code

Date.prototype.format = function (format) {
    format = format || "yyyy-MM-dd";
    var args = {//from   ww w  .ja v a2s .  c o  m
        "M+": this.getMonth() + 1,
        "d+": this.getDate(),
        "h+": this.getHours(),
        "m+": this.getMinutes(),
        "s+": this.getSeconds(),
        "q+": Math.floor((this.getMonth() + 3) / 3),  //quarter
        "S": this.getMilliseconds()
    };
    if (/(y+)/.test(format))
        format = format.replace(RegExp.$1, (this.getFullYear() + "").substr(4 - RegExp.$1.length));
    for (var i in args) {
        var n = args[i];
        if (new RegExp("(" + i + ")").test(format))
            format = format.replace(RegExp.$1, RegExp.$1.length == 1 ? n : ("00" + n).substr(("" + n).length));
    }
    return format;
};

Related

  1. format(format)
    'use strict'
    const fs = require('fs');
    const path = require('path');
    Date.prototype.format = function(format) {
        let o = {
            "M+": this.getMonth() + 1,
            "d+": this.getDate(),
            "H+": this.getHours(),
            "m+": this.getMinutes(),
    ...
    
  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), 
    ...
    
  3. 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()
    ...
    
  4. 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':
    ...
    
  5. 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()
    ...
    
  6. 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, 
    ...
    
  7. 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();
    ...
    
  8. 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(), 
    ...
    
  9. 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(),
    ...