Skip to content

Commit a32ba3f

Browse files
committed
fix: Calendar days shifting depending on the OS timezone (fixes #1198)
1 parent 858ea3c commit a32ba3f

File tree

2 files changed

+8
-9
lines changed

2 files changed

+8
-9
lines changed

src/VueDatePicker/components/DatePicker/DpCalendar.vue

Lines changed: 6 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -116,7 +116,7 @@
116116
<script lang="ts" setup>
117117
import { computed, nextTick, onMounted, onUnmounted, ref, useTemplateRef } from 'vue';
118118
import { unrefElement, useSwipe } from '@vueuse/core';
119-
import { getISOWeek, getWeek, set, type Day } from 'date-fns';
119+
import { getISOWeek, getWeek, set, type Day, startOfWeek, endOfWeek, eachDayOfInterval } from 'date-fns';
120120
121121
import { useArrowNavigation, useHelperFns, useDateUtils, useContext, useFormatter } from '@/composables';
122122
@@ -376,14 +376,13 @@
376376
};
377377
378378
const getDayNames = (): string[] => {
379-
const daysArray = [1, 2, 3, 4, 5, 6, 7];
379+
const now = getDate();
380+
const start = startOfWeek(now, { locale: rootProps.locale, weekStartsOn: +rootProps.weekStart as Day });
381+
const end = endOfWeek(now, { locale: rootProps.locale, weekStartsOn: +rootProps.weekStart as Day });
380382
381-
const days = daysArray.map((day) => formatWeekDay(day));
383+
const daysInWeek = eachDayOfInterval({ start, end });
382384
383-
const beforeWeekStart = days.slice(0, +rootProps.weekStart);
384-
const afterWeekStart = days.slice(+rootProps.weekStart + 1, days.length);
385-
386-
return [days[+rootProps.weekStart]!].concat(...afterWeekStart).concat(...beforeWeekStart);
385+
return daysInWeek.map((day) => formatWeekDay(day));
387386
};
388387
389388
defineExpose({ triggerTransition });

src/VueDatePicker/composables/useFormatter.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -18,8 +18,8 @@ export const useFormatter = () => {
1818
return format(setMonth(getDate(), month), formats.value.month, { locale: rootProps.locale });
1919
};
2020

21-
const formatWeekDay = (day: number) => {
22-
return format(getDate(`2017-01-0${day}T00:00:00+00:00`), formats.value.weekDay, { locale: rootProps.locale });
21+
const formatWeekDay = (date: Date) => {
22+
return format(date, formats.value.weekDay, { locale: rootProps.locale });
2323
};
2424

2525
const formatQuarter = (quarter: Date) => {

0 commit comments

Comments
 (0)