00016.取得当前日期并格式化

2022-07-11  本文已影响0人  笑着字太黑
Date.prototype.Format = function (fmt) {
  var o = {
    'M+': this.getMonth() + 1,
    'd+': this.getDate(),
    'H+': this.getHours(),
    'm+': this.getMinutes(),
    's+': this.getSeconds(),
    'S+': this.getMilliseconds()
  };

  var fmtMatch = fmt.match(/(y+)/);
  if (fmtMatch) {
    fmt = fmt.replace(fmtMatch[0], (this.getFullYear() + '').substring(4 - fmtMatch[0].length));
  }
  for (var k in o) {
    fmtMatch = fmt.match('(' + k + ')');
    if (fmtMatch) {
      fmt = fmt.replace(fmtMatch[0], (fmtMatch[0].length == 1) ? (o[k]) : (('00' + o[k]).substring(String(o[k]).length)));
    }
  }
  return fmt;
};

var df = new Date().Format('yyyy-MM-dd HH:mm:ss');
console.log(`df:${df}`);
上一篇 下一篇

猜你喜欢

热点阅读