1、获取n月前或n月后的日期 (适用于yy-MM)
/**
* @author ljw
* @description Date格式化
* @warn 如果没有传入时间,就返回当前时间的格式化数据 注意年初一月调用传参
* @param {
* date 传入日期 不传默认为当前日期
* elip 获取n月前或n月后的日期
* }
*/
dateByMonthFormat(date, elip) {
if (!date) date = new Date();
let ydel = 0;
let mdel = 0;
if (elip && Math.abs(elip / 12) >= 1) {
ydel = elip / Math.floor(Math.abs(elip / 12));
}
if (elip && Math.abs(elip) >= 1) {
mdel = elip - ydel;
}
if (date) {
let newDate = new Date(date);
let y = newDate.getFullYear();
let m = newDate.getMonth() + 1;
// 如果当前为1月份 - 特殊处理
if ([1].includes(m)) {
// 获取n月前
if (elip) {
y = y - 1;
m = 12;
}
} else {
// 如果存在elip
elip > 0 ? (y = y + ydel) : (y = y - ydel);
m = m + mdel;
}
console.log(m);
return y + "-" + this.addZero(m);
}
},
使用
// 当前时间为 0 上个月为 -1 下个月为 1,依次递推。
this.dateByMonthFormat(null, -1) // 例:return 2021-11 当前时间为 2021-12
2、对【1】的进一步封装,可指定日期 day ,(适用于yy-MM-dd)
/**
* @author ljw
* @description Date格式化
* @warn 如果没有传入时间,就返回当前时间的格式化数据
* @param {
* date 传入日期 不传默认为当前日期
* elip 获取n月前或n月后的日期
* day 指定当前月内任何一天
* }
*/
dateByDayFormat(date, elip, day) {
if (!date) date = new Date();
let ydel = 0;
let mdel = 0;
if (elip && Math.abs(elip / 12) >= 1) {
ydel = elip / Math.floor(Math.abs(elip / 12));
}
if (elip && Math.abs(elip) >= 1) {
mdel = elip - ydel;
}
if (date) {
let newDate = new Date(date);
let y = newDate.getFullYear();
let m = newDate.getMonth() + 1;
let d = date.getDate();
let time = new Date();
time.setFullYear(y);
time.setMonth(m); // 设置当前月
time.setDate(0);
if ([1, 3].includes(m)) {
y = m == 1 ? y - 1 : y;
m = m == 1 ? 12 : (m = 3 ? 2 : m);
let dStr = `${y}-${m}`;
let uyear = dStr.substring(0, 4); //年
let umonth = dStr.substring(5, 7); //月
let ud = new Date(uyear, umonth, 0);
let daysCount = ud.getDate(); // 当前月总天数
if (day === "start") {
// 1号
d = "01";
} else if (day === "end") {
// 最后一天
d = this.addZero(daysCount);
} else if (!day) {
d = "01"; // 不传默认为1号
} else {
d = this.addZero(day);
}
} else {
elip > 0 ? (y = y + ydel) : (y = y - ydel);
m = m + mdel;
let dStr = `${y}-${m}`;
let uyear = dStr.substring(0, 4); //年
let umonth = dStr.substring(5, 7); //月
let ud = new Date(uyear, umonth, 0);
let daysCount = ud.getDate(); // 当前月总天数
if (day === "start") {
// 1号
d = "01";
} else if (day === "end") {
// 最后一天
console.log('daysCount :>> ', daysCount);
d = this.addZero(daysCount);
} else if (!day) {
d = "01"; // 不传默认为1号
} else {
d = this.addZero(day);
}
}
console.log(y + "-" + this.addZero(m) + "-" + this.addZero(d));
return y + "-" + this.addZero(m) + "-" + this.addZero(d);
}
},
使用
// 当前时间为 0 上个月为 -1 下个月为 1,依次递推。
// 月起始日
this.dateByDayFormat(null, -1, "start") // 例:return 2021-11-01 当前时间为 2021-12-10
// 月结束日
this.dateByDayFormat(null, -1, "end") // 例:return 2021-11-31 当前时间为 2021-12-10
// 月指定日
this.dateByDayFormat(null, -1, "22") // 例:return 2021-11-22 当前时间为 2021-12-10
// 月默认日【不传day或传null】,默认为月起始日
this.dateByDayFormat(null, -1) // 例:return 2021-11-01 当前时间为 2021-12-10
this.dateByDayFormat(null, -1, null) // 例:return 2021-11-01 当前时间为 2021-12-10
辅助函数
1、补0
addZero(val) {
return Number(val) < 10 ? "0" + Number(val) : Number(val);
},