Nodejs Date Format format(fmt)

Here you can find the source of format(fmt)

Method Source Code

// javascript file

Date.DATETIME_FORMAT = "yyyy-MM-dd hh:mm:ss";
Date.DATE_FORMAT = "yyyy-MM-dd";

Date.prototype.format = function(fmt) {
    var o = {//from  w ww  . j  a v a 2  s.  c  o  m
        y: this.getFullYear(),
        M: this.getMonth() + 1,
        d: this.getDate(),
        h: this.getHours(),
        m: this.getMinutes(),
        s: this.getSeconds()
    }; 
 
    return fmt.replace(/(y+|M+|d+|h+|m+|s+)/g, function(e) {
        var key = e.charAt(0);
        if (e.length > 1) {
            o[key] = "0" + o[key];
        }
        return (o[key].toString().slice(-(e.length > 2 ? e.length : 2)));
    }); 
}

Related

  1. format(f)
    Date.prototype.format = function(f) {
        if (!this.valueOf()) return " ";
        var weekName = ["?", "?", "?", "?", "?", "?", "?"];
        var d = this;
        return f.replace(/(yyyy|yy|MM|dd|E|hh|mm|ss|a\/p)/gi, function($1) {
            switch ($1) {
                case "yyyy": return d.getFullYear();
                case "yy": return (d.getFullYear() % 1000).zf(2);
                case "MM": return (d.getMonth() + 1).zf(2);
    ...
    
  2. format(f)
    var gsMonthNames = new Array(
    'January', 'February', 'March', 'April', 'May', 'June', 'July', 'August', 'September', 'October', 'November', 'December'
    );
    var gsDayNames = new Array(
    'Sunday', 'Monday', 'Tuesday', 'Wednesday', 'Thursday', 'Friday', 'Saturday'
    );
    Date.prototype.format = function (f) {
        if (!this.valueOf())
            return ' ';
    ...
    
  3. format(f)
    Date.prototype.format = function(f) {
        if (!this.valueOf()) return " ";
        var weekName = [
            'Sunday',
            'Monday',
            'Tuesday',
            'Wednesday',
            'Thursday',
            'Friday',
    ...
    
  4. format(f)
    var DayNames = new Array('Sunday', 'Monday', 'Tuesday', 'Wednesday', 'Thursday',
                'Friday', 'Saturday');
    var MonthNames = new Array('January','February','March','April','May','June',
                'July', 'August', 'September', 'October', 'November', 'December');
    if(!encodeURIComponent) {
        encodeURIComponent = function(str){ return escape(str); };
        decodeURIComponent = function(str){ return unescape(str); };
    };
    Date.prototype.format = function(f) {
    ...
    
  5. format(fmt)
    Date.prototype.format = function (fmt) {
        var date = this;
        return fmt.replace(
            /\{([^}:]+)(?::(\d+))?\}/g,
            function (s, comp, pad) {
                var fn = date["get" + comp];
                if (fn) {
                    var v = (fn.call(date) +
                        (/Month$/.test(comp) ? 1 : 0)).toString();
    ...
    
  6. format(fmt)
    Date.prototype.format = function (fmt) {
      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()
    ...
    
  7. format(fmt)
    Date.prototype.format = function (fmt) {
      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()
    ...
    
  8. format(fmt)
    Date.prototype.format = function (fmt) {
      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() 
    ...
    
  9. format(fmt)
    Date.prototype.format = function(fmt) {
      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()
    ...