Skip to main content

格式化日期

日期转换 YYYYMMDD

方法一

 function dateFormat() {
let nowDate = new Date()
let year = nowDate.getFullYear()
let month = nowDate.getMonth() + 1
let day = nowDate.getDate()
if (month < 10) month = '0' + month
if (day < 10) day = '0' + day
return year+month+day
}
console.log(dateFormat())

方法二

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();

链接

JavaScript - 获取时间并且转换成yyyy-MM-dd形式 JS日期对象获取YYYYMMDD格式 js怎么格式化日期 js 日期时间的格式化 JS日期格式化转换方法 日期格式化js