Skip to content

fix: fix timezone issues for getMonths function #818

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
wants to merge 1 commit into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 5 additions & 5 deletions src/VueDatePicker/utils/util.ts
Original file line number Diff line number Diff line change
Expand Up @@ -25,15 +25,15 @@ export const getArrayInArray = <T>(list: T[], increment = 3): T[][] => {

function dayNameIntlMapper(locale: string) {
return (day: number) => {
return new Intl.DateTimeFormat(locale, { weekday: 'short', timeZone: 'UTC' })
.format(new Date(`2017-01-0${day}T00:00:00+00:00`))
return new Intl.DateTimeFormat(locale, { weekday: 'short' })
.format(new Date(`2017-01-0${day}T00:00:00`))
.slice(0, 2);
};
}

function dayNameDateFnsMapper(formatLocale: Locale) {
return (day: number) => {
return format(new Date(`2017-01-0${day}T00:00:00+00:00`), 'EEEEEE', { locale: formatLocale });
return format(new Date(`2017-01-0${day}T00:00:00`), 'EEEEEE', { locale: formatLocale });
};
}

Expand Down Expand Up @@ -86,7 +86,7 @@ export const getMonths = (
): IDefaultSelect[] => {
const months = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12].map((month) => {
const mm = month < 10 ? `0${month}` : month;
return new Date(`2017-${mm}-01T00:00:00+00:00`);
return new Date(`2017-${mm}-01T00:00:00`);
});

if (formatLocale !== null) {
Expand All @@ -104,7 +104,7 @@ export const getMonths = (
}
}

const formatter = new Intl.DateTimeFormat(locale, { month: monthFormat, timeZone: 'UTC' });
const formatter = new Intl.DateTimeFormat(locale, { month: monthFormat });
return months.map((date, i) => {
const month = formatter.format(date);
return {
Expand Down
9 changes: 7 additions & 2 deletions tests/unit/timezone.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,9 @@ describe('Timezone functionality', () => {
const offset = getOffset(local);
const zoned = localToTz(local, 'UTC');

expect(differenceInHours(local, zoned)).toEqual(offset);
const diff = Math.abs(differenceInHours(local, zoned, { roundingMethod: 'round' }));

expect(diff).toEqual(offset);
});

it('Should map date to a timezone depending on the timezone config', () => {
Expand All @@ -20,7 +22,10 @@ describe('Timezone functionality', () => {
const utcExact = new Date(getYear(today), 0, 15, 1, 0, 0, 0);
const offset = getOffset(today);
const dateInTz = sanitizeDateToLocal(today, { timezone: 'UTC', exactMatch: false });
expect(differenceInHours(today, dateInTz as Date)).toEqual(offset);

const diff = Math.abs(differenceInHours(today, dateInTz as Date, { roundingMethod: 'round' }));

expect(diff).toEqual(offset);

const dateExact = sanitizeDateToLocal(utc, {
timezone: 'UTC',
Expand Down
Loading