我正在實施一個簡單的日歷,但我對所有非閏熱月份都有問題。
如果您單擊下一步直到 2023 年 2 月,則顯示三月,如果您前進到 2024 年(這是一個飛躍),一切正常,我該如何解決?
let nav = 0;
let test = () => {
const dt = new Date();
dt.setMonth(new Date().getMonth() nav);
const day = dt.getDate();
const month = dt.getMonth();
const year = dt.getFullYear();
const monthName = `${dt.toLocaleDateString("en", { month: "long" })} ${year}`;
document.getElementById('feedBack').innerHTML = 'Nav: ' nav ' - Day: ' 1 ' - Month: ' month ' ( <b>' monthName '</b> ) - Year: ' year;
}
test();
#feedBack {margin-top:10px}
<button onclick="nav--;test()">Prev</button>
<button onclick="nav=0;test();">Current</button>
<button onclick="nav ;test();">Next</button>
<div id="feedBack"></div>
uj5u.com熱心網友回復:
等兩天問題就解決了嘿嘿(今天是29/06,你在迭代,29/07、29/08等等)。
或者您可以在增加月份之前更改月份物件。
let nav = 0;
let test = () => {
const dt = new Date();
dt.setDate(1); // this will move to the first day of the current month
dt.setMonth(new Date().getMonth() nav);
const month = dt.getMonth();
const year = dt.getFullYear();
const monthName = `${dt.toLocaleDateString("en", { month: "long" })} ${year}`;
document.getElementById('feedBack').innerHTML = 'Nav: ' nav ' - Day: ' 1 ' - Month: ' month ' ( <b>' monthName '</b> ) - Year: ' year;
}
test();
#feedBack {margin-top:10px}
<button onclick="nav--;test()">Prev</button>
<button onclick="nav=0;test();">Current</button>
<button onclick="nav ;test();">Next</button>
<div id="feedBack"></div>
轉載請註明出處,本文鏈接:https://www.uj5u.com/gongcheng/497599.html
標籤:javascript
上一篇:資料子集上的視窗函式
