Javascript Date format(format)

Javascript Date format(format)


Date.prototype.format = function f(format) {
/*//from ww  w. j a  va 2  s  . c om
 * format='yyyy-MM-dd hh:mm:ss';
 */

    const 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(),
    };

    if (/(y+)/.test(format)) {
        format = format.replace(
            RegExp.$1,
            (`${this.getFullYear()}`).substr(4 - RegExp.$1.length),
        );
    }

    // for (const k in o) {
    Object.keys(o).forEach((k) => {
        if (new RegExp(`(${k})`).test(format)) {
            format = format.replace(
                RegExp.$1,
                RegExp.$1.length === 1 ? o[k] : (`00${o[k]}`).substr((`${o[k]}`).length),
            );
        }
    });
    return format;
};

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

Javascript Date 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;// w  ww . ja  v  a 2s . c  o m
};

Javascript Date format(format)

Date.prototype.format=function(format){
 var o={// ww w  .jav a2s.c o m
  "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]);
 }
 return format;

}

Javascript Date format(format)

Date.prototype.format = function(format) {
  let m = {//from   w  w w  .ja  v a2 s .  c  om
    'y+': this.getFullYear(),// year
    'M+': this.getMonth() + 1, // month
    'd+': this.getDate(), // day
    'h+': this.getHours(), // hour
    'm+': this.getMinutes(), // minute
    's+': this.getSeconds(), // second
  }
  for(let k in m) {
    if(new RegExp(`(${k})`).test(format)) {
      let x = RegExp.$1
      let y = m[k] + ''
      let z = x.length > y.length
        ? y.padStart(x.length, '0')
        : y.substr(y.length - x.length)
      format = format.replace(x, z)
    }
  }
  return format
}

Javascript Date format(format)

/**/*  ww w . j  ava  2s  .c  o  m*/
 * Created by sunshine on 12/8/15.
 */
Date.prototype.format = function (format) {
    var o = {
        "M+": this.getMonth() + 1,
        // month
        "d+": this.getDate(),
        // day
        "h+": this.getHours(),
        // hour
        "m+": this.getMinutes(),
        // minute
        "s+": this.getSeconds(),
        // second
        "q+": Math.floor((this.getMonth() + 3) / 3),
        // quarter
        "S": this.getMilliseconds()
        // millisecond
    };
    if (/(y+)/.test(format) || /(Y+)/.test(format)) {
        format = format.replace(RegExp.$1, (this.getFullYear() + "").substr(4 - RegExp.$1.length));
    }
    for (var k in o) {
        if (new RegExp("(" + k + ")").test(format)) {
            format = format.replace(RegExp.$1, RegExp.$1.length == 1 ? o[k] : ("00" + o[k]).substr(("" + o[k]).length));
        }
    }
    return format;
};

Javascript Date format(format)

Date.prototype.format = function(format) {
 var o = {//  w  w w.  ja  va2s .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(format)) {
  format = format.replace(RegExp.$1, (this.getFullYear() + "").substr(4
      - RegExp.$1.length));
 }
 for (var k in o) {
  if (new RegExp("(" + k + ")").test(format)) {
   format = format.replace(RegExp.$1, RegExp.$1.length == 1
       ? o[k]
       : ("00" + o[k]).substr(("" + o[k]).length));
  }
 }
 return format;
}

function getFormatDate(date, pattern) {
 if (date == undefined) {
  date = new Date();
 }
 if (pattern == undefined) {
  pattern = "yyyy-MM-dd hh:mm:ss";
 }
 return date.format(pattern);
}

function formatDate(date) {
 var fdate = date.format("yyyy-MM-dd hh:mm:ss")
 return fdate;
}

Javascript Date format(format)

var Common = {/* w ww  .j  av  a  2 s.  c  om*/
    init: function () {
        console.log("common.js is runnning...");
    },
    getRootPath: function () {
        return contextPath;
    },
    validationFail:function(msg,dom){
     layer.tips(msg,dom);
    }
}

Date.prototype.format = function (format) {
    var date = {
        "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+)/i.test(format)) {
        format = format.replace(RegExp.$1, (this.getFullYear() + '').substr(4 - RegExp.$1.length));
    }
    for (var k in date) {
        if (new RegExp("(" + k + ")").test(format)) {
            format = format.replace(RegExp.$1, RegExp.$1.length == 1 ? date[k] : ("00" + date[k]).substr(("" + date[k]).length));
        }
    }
    return format;
}



PreviousNext

Related