Nodejs Date Format formatf(format)

Here you can find the source of formatf(format)

Method Source Code

Date.prototype.format = function f(format) {
/*/*w ww .j av  a  2s .  c  o m*/
 * 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;
};

Related

  1. formatMMDDYYYY()
    Date.prototype.formatMMDDYYYY = function () {
       return this.getMonth() + 1 + '/' + this.getDate() + '/' + this.getFullYear();
    };
    
  2. formatStr()
    Date.prototype.formatStr=function(){
        function convert2fixed(str){
            var _num=Number(str);
            if(!isNaN(str)){
                if(_num<10){
                    return '0'+_num;
                }else
                return _num;
    ...
    
  3. formatTime(date)
    function formatTime(date) {
      if(date instanceof Date === false) {
        return console.log('err');
      var hour = pad(date.getHours());
      var minute = pad(date.getMinutes());
      return [hour, minute].join(':');
    
  4. formatTime(pattern)
    Date.prototype.formatTime = function (pattern) {
        pattern = pattern.replace(/hh/g, this.getHours().toString().padLeft(2, 0));
        pattern = pattern.replace(/mm/g, this.getMinutes().toString().padLeft(2, 0));
        return pattern;
    };
    
  5. formateDate()
    Date.prototype.formateDate = function() {
      var mm = this.getMonth() + 1;
      var dd = this.getDate();
      return [this.getFullYear(), !mm[1] && '0', mm, !dd[1] && '0', dd].join('');
    };
    
  6. formattedDate()
    Date.prototype.formattedDate = function(){
      var dd   = (this.getDate() < 10 ? "0" : "") + this.getDate();
      var mm   = (this.getMonth() + 1 < 10 ? "0" : "") + (this.getMonth() + 1);
      var yyyy = this.getFullYear();
      return yyyy + '/' + mm + '/' + dd;
    
  7. formattedDate(delemeter)
    Date.prototype.formattedDate = function (delemeter) {
      var dm = delemeter;
      if (!dm) dm = '';
      var yyyy = this.getFullYear().toString();
      var mm = (this.getMonth() + 1).toString();
      var dd = this.getDate().toString();
      if (mm < 10) mm = "0" + mm;
      if (dd < 10) dd = "0" + dd;
      return "" + yyyy + dm + mm + dm + dd;
    ...
    
  8. formattedTime()
    Date.prototype.formattedTime = function(){
      var hours   = (this.getHours() < 10 ? "0" : "")   + this.getHours();
      var minutes = (this.getMinutes() < 10 ? "0" : "") + this.getMinutes();
      var seconds = (this.getSeconds() < 10 ? "0" : "") + this.getSeconds();
      return hours + ':' + minutes + ':' + seconds
    
  9. fromISO(s)
    Date.prototype.fromISO= function(s){
      var day, tz, rx=  /^(\d{4}\-\d\d\-\d\d([tT][\d:\.]*)?)([zZ]|([+\-])(\d\d):(\d\d))?$/, p= rx.exec(s) || [];
      if(p[1]){
        day= p[1].split(/\D/).map(function(itm){
          return parseInt(itm, 10) || 0;
        });
        day[1]-= 1;
        day= new Date(Date.UTC.apply(Date, day));
        if(!day.getDate()) return NaN;
    ...