javascript - js月份切換,求思路或代碼
問(wèn)題描述
// 時(shí)間切換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)); // 輸出時(shí)間 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);}
問(wèn)題解答
回答1:如果month代表當(dāng)前月份,通過(guò) new Date(year, month-1) 獲取上/下一個(gè)月,new Date(year, month, 0)獲取上個(gè)月的最后一天,new Date(year, month, 1)獲取當(dāng)前月的第一天,new Date(year, month+1, 0)獲取當(dāng)前月的最后一天(也就是當(dāng)前月的總天數(shù))。然后通過(guò)這些值來(lái)計(jì)算切換后的月份天數(shù)和第一個(gè)星期前面的縮進(jìn)天數(shù),最后渲染出來(lái)
