Nodejs Date Format format(fmt)

Here you can find the source of format(fmt)

Method Source Code

Date.prototype.format = function(fmt) {
   var o = {//from  www .  j a v  a 2  s .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), 
      "S" : this.getMilliseconds()
   };
   if (/(y+)/.test(fmt))
      fmt = fmt.replace(RegExp.$1, (this.getFullYear() + "").substr(4 - RegExp.$1.length));
   for ( var k in o)
      if (new RegExp("(" + k + ")").test(fmt))
         fmt = fmt.replace(RegExp.$1, (RegExp.$1.length == 1) ? (o[k]) : (("00" + o[k]).substr(("" + o[k]).length)));
   return fmt;
}

Related

  1. 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();
    ...
    
  2. 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(),
    ...
    
  3. 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()
    ...
    
  4. 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()
    ...
    
  5. 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() 
    ...
    
  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)
    'use strict';
    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),
    ...
    
  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)
    var d1 = new Date();
    console.log( d1 );
    console.log( d1.toString() );
    console.log( d1.toDateString() );
    console.log( d1.toLocaleDateString() );
    console.log( d1.toTimeString() );
    console.log( d1.toLocaleTimeString() );
    Date.prototype.format = function(fmt){
      if(!fmt){
    ...