Nodejs Date Format format(f)

Here you can find the source of format(f)

Method Source Code

/**/*  ww  w  . j a v a2  s .  c  om*/
 * @fileoverview Holds a couple helper functions that might
 * come in handy
 *
 * @author Andrew Huynh
 */
 
/** Days of the week @final @type {String[]}*/
var DayNames = new Array('Sunday', 'Monday', 'Tuesday', 'Wednesday', 'Thursday',
            'Friday', 'Saturday');

/** Names of months @final @type {String[]} */
var MonthNames = new Array('January','February','March','April','May','June',
            'July', 'August', 'September', 'October', 'November', 'December');

//===============================================
// Ensure that decode/encodeURIComponent is available to use.
if(!encodeURIComponent) {
    encodeURIComponent = function(str){ return escape(str); };
    decodeURIComponent = function(str){ return unescape(str); };
};

/**
 * Adds a format method to the Date option.
 * @addon
 */
Date.prototype.format = function(f) {
    if (!this.valueOf()) return ' ';
    
    var d = this;

    return f.replace(/(yyyy|mmmm|mmm|mm|dddd|ddd|dd|hh|nn|ss|a\/p)/gi,
        function($1)
        {
            switch ($1.toLowerCase())
            {
                case 'yyyy': return d.getFullYear();
                case 'mmmm': return MonthNames[d.getMonth()];
                case 'mmm':  return MonthNames[d.getMonth()].substr(0, 3);
                case 'mm':   return (d.getMonth() + 1).zf(2);
                case 'dddd': return DayNames[d.getDay()];
                case 'ddd':  return DayNames[d.getDay()].substr(0, 3);
                case 'dd':   return d.getDate().zf(2);
                case 'hh':   return ((h = d.getHours() % 12) ? h : 12).zf(2);
                case 'nn':   return d.getMinutes().zf(2);
                case 'ss':   return d.getSeconds().zf(2);
                case 'a/p':  return d.getHours() < 12 ? 'a' : 'p';
            }
        }
    );
}

/**
 * Adds a trim function to the String object. Trims white space at the
 * beginning & end of the string
 * @addon
 */
String.prototype.trim = function() {
    a = this.replace(/^\s+/, '');
    return a.replace(/\s+$/, '');
};

/**
 * Easy-to-use random number generator
 * @param {int} start Lower bound of random number generator
 * @param {int} end Upper bound of random number generator
 */
function randInt(start, end) {
    return Math.floor(Math.random()*(end-start)) + start;
}

Related

  1. 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("-");
    };
    
  2. 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);
    };
    ...
    
  3. 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);
    ...
    
  4. 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 ' ';
    ...
    
  5. format(f)
    Date.prototype.format = function(f) {
        if (!this.valueOf()) return " ";
        var weekName = [
            'Sunday',
            'Monday',
            'Tuesday',
            'Wednesday',
            'Thursday',
            'Friday',
    ...
    
  6. 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();
    ...
    
  7. 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(),
    ...
    
  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()
    ...