Nodejs Date Format format(f)

Here you can find the source of format(f)

Method Source Code

Date.prototype.format = function(f) {
    if (!this.valueOf()) return " ";

    var weekName = [
        'Sunday',
        'Monday',
        'Tuesday',
        'Wednesday',
        'Thursday',
        'Friday',
        'Saturday'
    ];/*from  w w  w. ja  va2s .  c  om*/
    var weekNameShort = [
        'Sunday',
        'Monday',
        'Tuesday',
        'Wednesday',
        'Thursday',
        'Friday',
        'Saturday'
    ];
    var monthName = [
        "January",
        "February",
        "March",
        "April",
        "May",
        "June",
        "July",
        "August",
        "September",
        "October",
        "November",
        "December"
    ];
    var monthNameShort = [
        "January",
        "February",
        "March",
        "April",
        "May",
        "June",
        "July",
        "August",
        "September",
        "October",
        "November",
        "December"
    ];
    var d = this;

    return f.replace(/(yyyy|yy|MMM|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);
            case "MMM": return monthNameShort[d.getMonth()];
            case "dd": return d.getDate().zf(2);
            case "E": return weekName[d.getDay()];
            case "e": return weekNameShort[d.getDay()];
            case "HH": return d.getHours().zf(2);
            case "hh": return ((h = d.getHours() % 12) ? h : 12).zf(2);
            case "mm": return d.getMinutes().zf(2);
            case "ss": return d.getSeconds().zf(2);
            case "a/p": return d.getHours() < 12 ? "AM" : "PM";
            default: return $1;
        }
    });
};

String.prototype.string = function(len){var s = '', i = 0; while (i++ < len) { s += this; } return s;};
String.prototype.zf = function(len){return "0".string(len - this.length) + this;};
Number.prototype.zf = function(len){return this.toString().zf(len);};

Related

  1. format()
    Date.prototype.format = function(){
      var formatMonth = function(month, numberOfDigits){
        month += 1;
        month = insertZerosLeft(month, numberOfDigits);
        return month;
      };
      var insertZerosLeft = function(data, numberOfDigits){
        data = data.toString();
        var zeros = '';
    ...
    
  2. format()
    Date.prototype.format = function () {
        var month = this.getMonth() + 1;
        var date = this.getDate();
        month < 10 && (month = "0" + month);
        date < 10 && (date = "0" + date);
        return [this.getFullYear(), month, date].join("-");
    };
    
  3. format(dateFormat)
    Date.prototype.format = function (dateFormat) {
        var day = this.getDate();
        var month = this.getMonth() + 1;
        var year = this.getFullYear();
        if (dateFormat === "dd/mm/yyyy")
            return '{0}/{1}/{2}'.format(day, month > 9 ? month : '0' + month, year);
    };
    ...
    
  4. 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);
    ...
    
  5. 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 ' ';
    ...
    
  6. 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) {
    ...
    
  7. 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();
    ...
    
  8. format(fmt)
    Date.DATETIME_FORMAT = "yyyy-MM-dd hh:mm:ss";
    Date.DATE_FORMAT = "yyyy-MM-dd";
    Date.prototype.format = function(fmt) {
        var o = {
            y: this.getFullYear(),
            M: this.getMonth() + 1,
            d: this.getDate(),
            h: this.getHours(),
            m: this.getMinutes(),
    ...
    
  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()
    ...