js获取今天、昨天、明天的日期函数代码(js获取当前日期的下一天)没想到

随心笔谈1年前 (2023)发布 admin
111 0

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”, “-“));

© 版权声明

相关文章