From 8eb4a5e455840bb8c8312e416f41cccbd5aa18e2 Mon Sep 17 00:00:00 2001 From: Marks Polakovs Date: Tue, 26 Sep 2023 17:23:10 +0100 Subject: [PATCH] fix(getWeeksBetween): correctly handle differing timezones --- src/calendar.ts | 7 ++++++- 1 file changed, 6 insertions(+), 1 deletion(-) diff --git a/src/calendar.ts b/src/calendar.ts index 287afab..5a1ff44 100644 --- a/src/calendar.ts +++ b/src/calendar.ts @@ -369,7 +369,12 @@ export const academicYears: AcademicYear[] = [ } ]; -export function getWeeksBetween(startDate: Date, endDate: Date): number { +export function getWeeksBetween(rawStartDate: Date, endDate: Date): number { + // startDate and endDate might not be in the same timezone (in particular around the BST transition) + let startDate = rawStartDate; + if (startDate.getTimezoneOffset() != endDate.getTimezoneOffset()) { + startDate = dayjs(startDate).add(startDate.getTimezoneOffset() - endDate.getTimezoneOffset(), 'minute').toDate(); + } return Math.abs(dayjs(endDate).diff(dayjs(startDate), 'weeks')); }