怎么递增递减获取上月下月的时间天数
具体什么意思 ,举个例子
不知道对不对,大概就是这个思路吧。
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Document</title>
</head>
<body>
<script>
/**
* 获取月份天数
*/
function getMonthDays(offset) {
let cur = new Date()
let tar = new Date(cur.getFullYear(), cur.getMonth() + offset + 1, 0)
console.log(tar.toDateString());
return tar.getDate();
}
console.log("四月", getMonthDays(0));
console.log("三月", getMonthDays(-1));
console.log("二月", getMonthDays(-2));
console.log("一月", getMonthDays(-3));
console.log("去年十二月", getMonthDays(-4));
</script>
</body>
</html>