美文网首页
00016.取得当前日期并格式化

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

作者: 笑着字太黑 | 来源:发表于2022-07-11 16:21 被阅读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}`);

相关文章

网友评论

      本文标题:00016.取得当前日期并格式化

      本文链接:https://www.haomeiwen.com/subject/dkpnbrtx.html