Skip to content

Commit

Permalink
fix(getWeeksBetween): correctly handle differing timezones
Browse files Browse the repository at this point in the history
  • Loading branch information
markspolakovs authored and chriscn committed Sep 26, 2023
1 parent 5f8ceff commit 8eb4a5e
Showing 1 changed file with 6 additions and 1 deletion.
7 changes: 6 additions & 1 deletion src/calendar.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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'));
}

Expand Down

0 comments on commit 8eb4a5e

Please sign in to comment.