Skip to content

Commit

Permalink
fix: include timezontimezonee adjustments
Browse files Browse the repository at this point in the history
  • Loading branch information
Sqrrl committed Jan 20, 2023
1 parent d0d3835 commit bb577fe
Show file tree
Hide file tree
Showing 4 changed files with 34 additions and 11 deletions.
5 changes: 5 additions & 0 deletions .changeset/fresh-foxes-clean.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
'wc-datepicker': patch
---

Fix timezone adjustments
4 changes: 2 additions & 2 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -24,8 +24,8 @@
"start": "yarn start:stencil ",
"start:stencil": "stencil build --dev --watch --serve",
"start:docs": "concurrently --kill-others \"npm-watch build:docs\" \"serve docs-www\"",
"test": "stencil test --spec",
"test:watch": "stencil test --spec --watchAll",
"test": "TZ=UTC stencil test --spec",
"test:watch": "TZ=UTC stencil test --spec --watchAll",
"generate": "stencil generate",
"changeset": "changeset",
"version-packages": "changeset version",
Expand Down
15 changes: 10 additions & 5 deletions src/components/wc-datepicker/wc-datepicker.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@ import {
getYear,
isDateInRange,
isSameDay,
removeTimezoneOffset,
subDays
} from '../../utils/utils';

Expand Down Expand Up @@ -123,7 +124,9 @@ export class WCDatepicker {

@Watch('startDate')
watchStartDate() {
this.currentDate = this.startDate ? new Date(this.startDate) : new Date();
this.currentDate = this.startDate
? removeTimezoneOffset(new Date(this.startDate))
: new Date();
}

@Watch('value')
Expand All @@ -145,7 +148,9 @@ export class WCDatepicker {
}

private init = () => {
this.currentDate = this.startDate ? new Date(this.startDate) : new Date();
this.currentDate = this.startDate
? removeTimezoneOffset(new Date(this.startDate))
: new Date();
this.updateWeekdays();
};

Expand Down Expand Up @@ -297,7 +302,7 @@ export class WCDatepicker {
return;
}

const date = new Date(target.dataset.date);
const date = removeTimezoneOffset(new Date(target.dataset.date));

this.updateCurrentDate(date);
this.onSelectDate(date);
Expand Down Expand Up @@ -371,8 +376,8 @@ export class WCDatepicker {
return;
}

const date = new Date(
(event.target as HTMLElement).closest('td').dataset.date
const date = removeTimezoneOffset(
new Date((event.target as HTMLElement).closest('td').dataset.date)
);

this.hoveredDate = date;
Expand Down
21 changes: 17 additions & 4 deletions src/utils/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -57,15 +57,18 @@ export function getDaysOfMonth(
}

export function getFirstOfMonth(date: Date): Date {
return new Date(
`${getYear(date)}-${String(getMonth(date)).padStart(2, '0')}-01`
const firstOfMonth = removeTimezoneOffset(
new Date(`${getYear(date)}-${String(getMonth(date)).padStart(2, '0')}-01`)
);

return firstOfMonth;
}

export function getISODateString(date: Date): string {
if (!(date instanceof Date)) {
return;
}

return `${date.getFullYear()}-${String(date.getMonth() + 1).padStart(
2,
'0'
Expand All @@ -87,7 +90,9 @@ export function getMonth(date: Date): number {

export function getMonths(locale?: string): string[] {
return new Array(12).fill(undefined).map((_, month) => {
const date = new Date(`2006-${String(month + 1).padStart(2, '0')}-01`);
const date = removeTimezoneOffset(
new Date(`2006-${String(month + 1).padStart(2, '0')}-01`)
);

return Intl.DateTimeFormat(locale, {
month: 'long'
Expand Down Expand Up @@ -143,7 +148,7 @@ export function getWeekDays(
.fill(undefined)
.map((_, index) => ((firstDayOfWeek + index) % 7) + 1)
.map((day) => {
const date = new Date(`2006-01-0${day}`);
const date = removeTimezoneOffset(new Date(`2006-01-0${day}`));

return [
Intl.DateTimeFormat(locale, {
Expand Down Expand Up @@ -183,6 +188,14 @@ export function isSameDay(date1?: Date, date2?: Date) {
);
}

export function removeTimezoneOffset(date: Date): Date {
const newDate = new Date(date);

newDate.setMinutes(newDate.getMinutes() + newDate.getTimezoneOffset());

return newDate;
}

export function subDays(date: Date, days: number): Date {
const newDate = new Date(date);

Expand Down

0 comments on commit bb577fe

Please sign in to comment.