且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

从 JS 日期对象获取 YYYYMMDD 格式的字符串?

更新时间:2023-11-06 18:16:52

修改了我经常使用的一段代码:

Altered piece of code I often use:

Date.prototype.yyyymmdd = function() {
  var mm = this.getMonth() + 1; // getMonth() is zero-based
  var dd = this.getDate();

  return [this.getFullYear(),
          (mm>9 ? '' : '0') + mm,
          (dd>9 ? '' : '0') + dd
         ].join('');
};

var date = new Date();
date.yyyymmdd();