Nodejs Date Format Format(fmt)

Here you can find the source of Format(fmt)

Method Source Code

/* eslint-disable no-extend-native */
Date.prototype.Format = function (fmt) {
  let values = {//from  w w  w. j  a  va2s .  co  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 key in values) {
    let value = values[key]
    if (new RegExp('(' + key + ')').test(fmt)) {
      fmt = fmt.replace(RegExp.$1, (RegExp.$1.length === 1) ? (value) : (('00' + value).substr(('' + value).length)))
    }
  }
  return fmt
}

Related

  1. splitYmd()
    Number.prototype.splitYmd=function(){
        var time=(this+"");
        var dates=[];
        dates.push(time.slice(0,4));
        dates.push(time.slice(4,6));
        dates.push(time.slice(6,8));
        return dates;
    
  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(),
            "i+": 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)
    Date.prototype.Format = function(fmt) {
        var k, o, str;
        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)
    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() 
    ...