javascript - js月份切換,求思路或代碼
問題描述
// 時間切換private tabTime(num: number) { let self = this; let now = new Date(); let arr = []; if (num > 0) {this.num2 ++; } if (num < 0) {this.num2 --; } let b = Number(now.getMonth()); now.setMonth(b + Number(this.num2)); // 輸出時間 let newA = now.getFullYear(); let newB: any = Number(now.getMonth()); let newC: any = now.getDay(); newB = ((newB + 1) < 10 ? '0' : '') + (newB + 1); let y = newA + ’ - ’ + (newB); this.currMonthData = y; let days = this.getEveryMonthDays(newA, newB); this.StartTimeStamp = Number(newA + newB + '01'); this.CurrTimeStamp = Number(newA + newB + days); this.loadChart(this.StartTimeStamp, this.CurrTimeStamp);}
問題解答
回答1:如果month代表當前月份,通過 new Date(year, month-1) 獲取上/下一個月,new Date(year, month, 0)獲取上個月的最后一天,new Date(year, month, 1)獲取當前月的第一天,new Date(year, month+1, 0)獲取當前月的最后一天(也就是當前月的總天數)。然后通過這些值來計算切換后的月份天數和第一個星期前面的縮進天數,最后渲染出來
