我有一個報告中只允許用戶選擇從最近3個月的選項,如下圖所示:獲取最近3個月的名字中選擇下拉
我想寫一個JavaScript函數爲了達成這個。
我想什麼:
document.write(getLastMonths(4));
它打印:
function getLastMonths(n) {
var months = new Array();
var today = new Date();
var year = today.getFullYear();
var month = today.getMonth() + 1;
var i = 0;
do {
months.push(year + (month > 9 ? "" : "0") + month);
if(month == 1) {
month = 12;
year--;
} else {
month--;
}
i++;
} while(i < n);
return months;
}
然後,我通過傳遞參數調用的函數
201704,201703,201702,201701
這是不是我需要它的模式英寸
任何人都可以幫助我嗎?
或使用全角框架......有點奇怪提出了一個庫,東西容易,因爲這 – VikingCode
@VikingCode爲什麼你覺得很奇怪?我對你的意見很感興趣。 – lv0gun9
lib是一組工具,通常是一大組工具(讀取文件),您可以將其導入到項目中,以使代碼更簡單,並將功能封裝在正確的軟件中。但在這種情況下,導入一個庫沒有額外的價值。您可以在vanilla js中編寫較短的代碼,並且免除您不必管理的額外依賴項目。 – VikingCode