封装的获取任意月底的函数
function hqyd(data) {
// 判断是否为闰年的函数
function isRunYear(year1) {
var flag = false;
if (year1 % 4 == 0 && year1 % 100 != 0 || year1 % 400 == 0) {
flag = true;
}
return flag;
}
// let y = new Date().getFullYear();
// let m = new Date().getMonth();
let y = data.getFullYear();
let m = data.getMonth() + 1;
if (m < 10) {
m = "0" + m;
}
if (m == "00") {
y = y - 1;
m = 12;
}
let result;
if (m == "02") {
if (isRunYear(y)) {
result = y + "-" + m + "-29";
} else {
result = y + "-" + m + "-28";
}
}else if (m == "04" || m == "06" || m == "09" || m == 11) {
result = y + "-" + m + "-30";
} else {
result = y + "-" + m + "-31";
}
return result;
}
console.log(hqyd(new Date("2024-02-01")))
结果:2024-02-29
console.log(hqyd(new Date("2023-02-01")))
结果:2023-02-28