js date-formatter

//这是一个用来格式化时间的扩展,只需要引用进来就可以,本案例是一个reactJS的写法
'use strict';
//this module is used to forma date
//if you want to extend more,you can add your code here
module.exports = function(module) {
  String.repeat = function(chr, count) {
    var str = "";
    for(var i = 0; i < count; i++) {
      str += chr;
    }
    return str;
  };

  String.prototype.padL = function(width, pad) {
    //not pad to left when width is 0
    if (!width || width < 1) {
      return this;
    }
    //if pad string is empty, pad a empty string here;
    if (!pad) {
      pad=" ";
    }
    var length = width - this.length;
    if (length < 1) {
      return this.substr(0,width);
    }
    return (String.repeat(pad,length) + this).substr(0,width);
  };

  String.prototype.padR = function(width, pad){
    //not pad to right when width is 0
    if (!width || width<1) {
      return this;
    }
    //if pad string is empty, pad a empty string here;
    if (!pad) {
      pad=" ";
    }
    var length = width - this.length;
    if (length < 1) {
      this.substr(0,width);
    }
    return (this + String.repeat(pad,length)).substr(0,width);
  };

  Date.prototype.formatDate = function(format){
    var date = this;
    //set a default formatter string
    if (!format) {
      format= "MM/dd/yyyy";
    }
    var month = date.getMonth() + 1;
    var year = date.getFullYear();
    format = format.replace("MM",month.toString().padL(2,"0"));

    if (format.indexOf("yyyy") > -1) {
      format = format.replace("yyyy",year.toString());
    } else if (format.indexOf("yy") > -1) {
      format = format.replace("yy",year.toString().substr(2,2));
    }
    format = format.replace("dd",date.getDate().toString().padL(2,"0"));
    var hours = date.getHours();
    if (format.indexOf("t") > -1) {
       if (hours > 11) {
          format = format.replace("t","pm");
       } else {
          format = format.replace("t","am");
       }
    }
    if (format.indexOf("HH") > -1) {
        format = format.replace("HH",hours.toString().padL(2,"0"));
    }
    if (format.indexOf("hh") > -1) {
        if (hours > 12) hours - 12;
        if (hours == 0) hours = 12;
        format = format.replace("hh",hours.toString().padL(2,"0"));
    }
    if (format.indexOf("mm") > -1) {
        format = format.replace("mm",date.getMinutes().toString().padL(2,"0"));
    }

    if (format.indexOf("ss") > -1) {
        format = format.replace("ss",date.getSeconds().toString().padL(2,"0"));
    }
    return format;
  };
};


//使用
var newTimer = new Date(new Date().getTime()).formatDate('yyyy-MM-dd HH:mm');

优化版

module.exports = function (timer, fmt = 'YYYY-MM-DD HH:mm:ss') {
  var date = new Date(timer)
  var o = {
    'M+': date.getMonth() + 1,
    'D+': date.getDate(),
    'h+': date.getHours() % 12 === 0 ? 12 : date.getHours() % 12,
    'H+': date.getHours(),
    'm+': date.getMinutes(),
    's+': date.getSeconds(),
    'q+': Math.floor((date.getMonth() + 3) / 3),
    'S': date.getMilliseconds()
  }
  var week = {
    '0': '\u65e5',
    '1': '\u4e00',
    '2': '\u4e8c',
    '3': '\u4e09',
    '4': '\u56db',
    '5': '\u4e94',
    '6': '\u516d'
  }
  if (/(Y+)/.test(fmt)) {
    fmt = fmt.replace(RegExp.$1, (date.getFullYear() + '').substr(4 - RegExp.$1.length))
  }
  if (/(E+)/.test(fmt)) {
    fmt = fmt.replace(RegExp.$1, ((RegExp.$1.length > 1) ? (RegExp.$1.length > 2 ? '\u661f\u671f' : '\u5468') : '') + week[date.getDay() + ''])
  }
  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
}

猜你喜欢

转载自blog.csdn.net/bule_chen/article/details/51382920