function getDays(date, type, fmt) {
let currentDate=new Date(date)
let y=currentDate.getFullYear()
let m=currentDate.getMonth() + 1
let d=currentDate.getDate()
function dateFormat(date, fmt) {
let y=new Date(date).getFullYear()
let m=new Date(date).getMonth() + 1
let d=new Date(date).getDate()
return `${y}${fmt}${m}${fmt}${d}`
}
switch (type) {
case “prev”:
if (d – 1 < 1) {
if (m – 1 < 1) {
y=y – 1
m=12
} else {
m=m – 1
}
d=new Date(y, m, 0).getDate()
} else {
d=d – 1
}
break
case “current”:
break
case “next”:
if (d + 1 > new Date(y, m, 0).getDate()) {
if (m + 1 > 12) {
y=y + 1
m=1
d=1
} else {
m=m + 1
d=1
}
} else {
d=d + 1
}
break;
default:
break;
}
return dateFormat(new Date(`${y}-${m}-${d}`), fmt)
}
console.log(getDays(new Date(‘2023-5-13’), “prev”, “-“));
console.log(getDays(new Date(‘2023-5-30’), “next”, “-“));
console.log(getDays(new Date(‘2023-5-31’), “next”, “-“));
let currentDate=new Date(date)
let y=currentDate.getFullYear()
let m=currentDate.getMonth() + 1
let d=currentDate.getDate()
function dateFormat(date, fmt) {
let y=new Date(date).getFullYear()
let m=new Date(date).getMonth() + 1
let d=new Date(date).getDate()
return `${y}${fmt}${m}${fmt}${d}`
}
switch (type) {
case “prev”:
if (d – 1 < 1) {
if (m – 1 < 1) {
y=y – 1
m=12
} else {
m=m – 1
}
d=new Date(y, m, 0).getDate()
} else {
d=d – 1
}
break
case “current”:
break
case “next”:
if (d + 1 > new Date(y, m, 0).getDate()) {
if (m + 1 > 12) {
y=y + 1
m=1
d=1
} else {
m=m + 1
d=1
}
} else {
d=d + 1
}
break;
default:
break;
}
return dateFormat(new Date(`${y}-${m}-${d}`), fmt)
}
console.log(getDays(new Date(‘2023-5-13’), “prev”, “-“));
console.log(getDays(new Date(‘2023-5-30’), “next”, “-“));
console.log(getDays(new Date(‘2023-5-31’), “next”, “-“));
© 版权声明
文章版权归作者所有,未经允许请勿转载。