Nodejs Date Format format( format )

Here you can find the source of format( format )

Method Source Code

define(function(require, exports, module) {

Date.prototype.format = function( format ){
    if( typeof format !== "string" ) return this.toLocaleString();
    var match = {
        "y": this.getFullYear(),
        "M": this.getMonth() + 1,
        "d": this.getDate(),
        "h": this.getHours(),
        "m": this.getMinutes(),
        "s": this.getSeconds(),
        "S": this.getMilliseconds()
    };//from   w w  w .ja  va  2  s .co  m
    return format.replace(/([ymdhs])+/ig, function(val, type, idx){
        return val.length === 1 ? match[type] : ("00" + match[type]).substr( ("00" + match[type]).length - val.length );
    });
}

});

Related

  1. dateFormat()
    var dateFormat = function () {
      var  token = /d{1,4}|m{1,4}|yy(?:yy)?|([HhMsTt])\1?|[LloSZ]|"[^"]*"|'[^']*'/g,
        timezone = /\b(?:[PMCEA][SDP]T|(?:Pacific|Mountain|Central|Eastern|Atlantic) (?:Standard|Daylight|Prevailing) Time|(?:GMT|UTC)(?:[-+]\d{4})?)\b/g,
        timezoneClip = /[^-+\dA-Z]/g,
        pad = function (val, len) {
          val = String(val);
          len = len || 2;
          while (val.length < len) val = "0" + val;
          return val;
    ...
    
  2. dateFormat(format)
    Date.prototype.dateFormat = function(format) {
        var yyyy = this.getFullYear().toString();
        format = format.replace(/yyyy/g, yyyy)
        var mm = (this.getMonth()+1).toString();
        format = format.replace(/mm/g, (mm[1]?mm:"0"+mm[0]));
        var dd  = this.getDate().toString();
        format = format.replace(/dd/g, (dd[1]?dd:"0"+dd[0]));
        var hh = this.getHours().toString();
        format = format.replace(/hh/g, (hh[1]?hh:"0"+hh[0]));
    ...
    
  3. dateFormat(value)
    function dateFormat(value) {
      if (!value)
        return new Date(0);
      var pattern = /\/Date\(([0-9]+)((-|\+)[0-9]+)?\)\
      var match = pattern.exec(value.toString());
      if (!match)
        return new Date(0);
      var utc = parseInt(match[1], 10);
      var offset = (parseInt(match[2], 10) || 0) * 10 * 60 * 60; 
    ...
    
  4. format
    ===
    
  5. format( format )
    Date.prototype.format = function( format ){
        if( typeof format !== "string" ) return this.toLocaleString();
        var match = {
            "y": this.getFullYear(),
            "M": this.getMonth() + 1,
            "d": this.getDate(),
            "h": this.getHours(),
            "m": this.getMinutes(),
            "s": this.getSeconds(),
    ...
    
  6. format(()
    Date.prototype.format = (function() {
      var
        that, txts,
        roman = ['I', 'II', 'III', 'IV', 'V', 'VI', 'VII', 'VIII', 'IX', 'X', 'XI', 'XII'],
        addZero = function(num) {
          return num < 10 ? '0' + num : num;
        },
        na = function(n, z) {
          return n % z;
    ...
    
  7. format()
    Date.prototype.format = function () {
      'use strict';
      var
          d = this.getDate()
        , m = this.getMonth() + 1
        , y = this.getFullYear();
        return '' + y + '-' + (m<=9 ? '0' + m : m) + '-' + (d <= 9 ? '0' + d : d);
    };
    
  8. format()
    function ISODateString(aDate) {
      function pad(aNumber) {
        return aNumber < 10 ? '0' + aNumber : aNumber;
      return aDate.getUTCFullYear() + '-' +
             pad(aDate.getUTCMonth() + 1) + '-' +
             pad(aDate.getUTCDate());
    Date.prototype.format = function () {
    ...
    
  9. format()
    function ISODateString(aDate) {
      function pad(aNumber) {
        return aNumber < 10 ? '0' + aNumber : aNumber;
      return aDate.getUTCFullYear() + '-' +
             pad(aDate.getUTCMonth() + 1) + '-' +
             pad(aDate.getUTCDate());
    Date.prototype.format = function () {
    ...