1.定义方法
fDate(t1, format = 'YYYY-MM-DD HH:mm:ss') {
const config = {
YYYY: t1.getFullYear(),
MM: (t1.getMonth() + 1).toString().padStart(2, '0'),
DD: t1.getDate().toString().padStart(2, '0'),
HH: t1.getHours().toString().padStart(2, '0'),
mm: t1.getMinutes().toString().padStart(2, '0'),
ss: t1.getSeconds().toString().toString().padStart(2, '0')
};
for (const key in config) {
format = format.replace(key, config[key]);
}
return format;
}
2.使用:
let times = this.fDate(new Date(), 'YYYY-MM-DD HH:mm:ss');
let time = this.fDate(new Date(), 'YYYY年MM月DD日 HH:mm:ss');
标签:DD,padStart,t1,HH,toString,格式,Date,new
From: https://www.cnblogs.com/domin520Jian/p/17775384.html