refactor: adjust calendar start date calculation to handle week alignment correctly

This commit is contained in:
Astrian Zheng 2025-02-19 16:41:03 +11:00
parent f55c72b535
commit 6b2af762f9
Signed by: Astrian
SSH Key Fingerprint: SHA256:rVnhx3DAKjujCwWE13aDl7uV6+9U1MvydLkNRXJrBiA

View File

@ -2,8 +2,11 @@ export default (currentDate: number, currentYear: number): Date[] => {
const baselineDate = new Date(currentYear, currentDate) const baselineDate = new Date(currentYear, currentDate)
const calendarStart = new Date(baselineDate.getFullYear(), baselineDate.getMonth(), 1) const calendarStart = new Date(baselineDate.getFullYear(), baselineDate.getMonth(), 1)
if (calendarStart.getDay() !== 1)
calendarStart.setDate(calendarStart.getDate() - calendarStart.getDay() + 1) const dayOfWeek = calendarStart.getDay() === 0 ? 7 : calendarStart.getDay()
if (dayOfWeek !== 1)
calendarStart.setDate(calendarStart.getDate() - dayOfWeek + 1)
const calendarEnd = new Date(baselineDate.getFullYear(), baselineDate.getMonth() + 1, 0) const calendarEnd = new Date(baselineDate.getFullYear(), baselineDate.getMonth() + 1, 0)
if (calendarEnd.getDay() !== 0) if (calendarEnd.getDay() !== 0)