文档:JS日期对象
Date.prototype.Format = function (fmt) {
let o = {
"M+": this.getMonth() + 1,
"d+": this.getDate(),
"H+": this.getHours(),
"m+": this.getMinutes(),
"s+": this.getSeconds(),
"q+": Math.floor((this.getMonth() + 3) / 3),
"S": this.getMilliseconds()
};
if (/(y+)/.test(fmt)) fmt = fmt.replace(RegExp.$1, (this.getFullYear() + "").substring(4 - RegExp.$1.length));
for (let k in o) {
if (new RegExp("(" + k + ")").test(fmt)) {
fmt = fmt.replace(RegExp.$1, (RegExp.$1.length == 1) ? (o[k]) : (("00" + o[k]).substring(("" + o[k]).length)));
}
}
return fmt;
}
export const getFormatDate = (date, type) => {
let curDate = date || new Date()
let newDate = new Date(curDate).Format(type)
return newDate
}
export const getYearAgoDate = (n = 0) => {
let curDate = new Date()
curDate.setFullYear(curDate.getFullYear() - n)
const ts = + curDate
return ts
}
export const getMonthAgoDate = (n = 0) => {
let curDate = new Date()
curDate.setMonth(curDate.getMonth() - n)
const ts = + curDate
return ts
}
export const getDayAgoDate = (n = 0) => {
let curDate = new Date();
let newDate = new Date(curDate - 1000 * 60 * 60 * 24 * n);
const ts = + newDate
return ts
}