Nodejs Date Format format(fmt)

Here you can find the source of format(fmt)

Method Source Code

var d1 = new Date();

console.log( d1 );/*from w ww  .  j  av  a2 s.c o  m*/
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){
      return undefined;
   }
   var year = this.getFullYear();
   var month = this.getMonth()+1;
   var date = this.getDate();
   var hour = this.getHours();
   var minute = this.getMinutes();
   var second = this.getSeconds();

   month = month<10 ? '0'+month : month;
   date = date<10 ? '0'+date : date;
   hour = hour<10 ? '0'+hour : hour;
   minute = minute<10 ? '0'+minute : minute;
   second = second<10 ? '0'+second : second;

   fmt = fmt.replace('yyyy', year);
   fmt = fmt.replace('MM', month);
   fmt = fmt.replace('dd', date);
   fmt = fmt.replace('HH', hour);
   fmt = fmt.replace('mm', minute);
   fmt = fmt.replace('ss', second);
   return fmt;
};

console.log();
console.log( d1.format() );
console.log( d1.format('yyyy') );
console.log( d1.format('yyyy-MM-dd') );
console.log( d1.format('yyyy?MM?dd?') );
console.log( d1.format('yyyy/MM/dd') );
console.log( d1.format('yyyy/MM/dd HH:mm:ss') );
console.log( d1.format('yyyy/MM/dd HH:mm') );

Related

  1. 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() 
    ...
    
  2. 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()
    ...
    
  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)
    '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),
    ...
    
  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(format)
    Date.prototype.format=function(format){
      var o={
        "M+":this.getMonth()+1,
        "d+":this.getDate(),
        "y+":this.getFullYear()
      };
      for(var k in o){
        if(new RegExp("("+k+")").test(format))
        format=format.replace(RegExp.$1,o[k]);
    ...
    
  7. format(format)
    Date.prototype.format = function(format) {
      var pad = function(str, num, wit) {str+="";for(var i = str.length; i < num; i++) {str=wit+str;}return str;};
      format = format.replace(/Y/, this.getFullYear());
      format = format.replace(/m/, pad(this.getMonth()+1, 2, 0));
      format = format.replace(/D/, pad(this.getDate(), 2, 0));
      format = format.replace(/H/, pad(this.getHours(), 2, 0));
      format = format.replace(/M/, pad(this.getMinutes(), 2, 0));
      format = format.replace(/S/, pad(this.getSeconds(), 2, 0));
      return format;
    ...
    
  8. format(format)
    Date.prototype.format = function(format) {
      var obj = {
        '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(format)
    Date.prototype.format = function(format){
    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() 
    ...