From fa37282fcb2029db90cc36c87432e2528ad744fe Mon Sep 17 00:00:00 2001 From: abouhatef Date: Tue, 24 Dec 2024 16:23:25 +0330 Subject: [PATCH] fixed dayjs another local and calendar --- packages/@mantine/dates/src/components/Day/Day.tsx | 2 +- .../Month/get-end-of-week/get-end-of-week.ts | 11 ++++++----- .../components/Month/get-month-days/get-month-days.ts | 8 +++++--- .../Month/get-start-of-week/get-start-of-week.ts | 10 +++++----- .../components/Month/is-same-month/is-same-month.ts | 8 ++++++-- 5 files changed, 23 insertions(+), 16 deletions(-) diff --git a/packages/@mantine/dates/src/components/Day/Day.tsx b/packages/@mantine/dates/src/components/Day/Day.tsx index d032934e90d..a8382a3c187 100644 --- a/packages/@mantine/dates/src/components/Day/Day.tsx +++ b/packages/@mantine/dates/src/components/Day/Day.tsx @@ -139,7 +139,7 @@ export const Day = factory((_props, ref) => { unstyled={unstyled} {...others} > - {renderDay?.(date) || date.getDate()} + {renderDay?.(date) || dayjs(date).date()} ); }); diff --git a/packages/@mantine/dates/src/components/Month/get-end-of-week/get-end-of-week.ts b/packages/@mantine/dates/src/components/Month/get-end-of-week/get-end-of-week.ts index 14a22479b4c..e68a9c45e75 100644 --- a/packages/@mantine/dates/src/components/Month/get-end-of-week/get-end-of-week.ts +++ b/packages/@mantine/dates/src/components/Month/get-end-of-week/get-end-of-week.ts @@ -1,12 +1,13 @@ +import dayjs from 'dayjs'; import type { DayOfWeek } from '../../../types'; export function getEndOfWeek(date: Date, firstDayOfWeek: DayOfWeek = 1) { - const value = new Date(date); - const lastDayOfWeek = firstDayOfWeek === 0 ? 6 : firstDayOfWeek - 1; + let value = dayjs(date) - while (value.getDay() !== lastDayOfWeek) { - value.setDate(value.getDate() + 1); + const lastDayOfWeek = firstDayOfWeek === 0 ? 6 : firstDayOfWeek - 1; + while (value.day() !== lastDayOfWeek) { + value = value.add(1, 'day'); } - return value; + return value.toDate(); } diff --git a/packages/@mantine/dates/src/components/Month/get-month-days/get-month-days.ts b/packages/@mantine/dates/src/components/Month/get-month-days/get-month-days.ts index f48f7acc503..dab1947fe72 100644 --- a/packages/@mantine/dates/src/components/Month/get-month-days/get-month-days.ts +++ b/packages/@mantine/dates/src/components/Month/get-month-days/get-month-days.ts @@ -1,3 +1,4 @@ +import dayjs from 'dayjs'; import { DayOfWeek } from '../../../types'; import { getEndOfWeek } from '../get-end-of-week/get-end-of-week'; import { getStartOfWeek } from '../get-start-of-week/get-start-of-week'; @@ -13,9 +14,10 @@ export function getMonthDays({ firstDayOfWeek = 1, consistentWeeks, }: GetMonthDaysInput): Date[][] { - const currentMonth = month.getMonth(); - const startOfMonth = new Date(month.getFullYear(), currentMonth, 1); - const endOfMonth = new Date(month.getFullYear(), month.getMonth() + 1, 0); + const day = dayjs(month) + const start = day.subtract(day.date() - 1, 'day') + const startOfMonth = start.toDate(); + const endOfMonth = start.add(+ day.daysInMonth() - 1, 'day').toDate(); const endDate = getEndOfWeek(endOfMonth, firstDayOfWeek); const date = getStartOfWeek(startOfMonth, firstDayOfWeek); const weeks: Date[][] = []; diff --git a/packages/@mantine/dates/src/components/Month/get-start-of-week/get-start-of-week.ts b/packages/@mantine/dates/src/components/Month/get-start-of-week/get-start-of-week.ts index 2bf51c5dc2e..13ba717a261 100644 --- a/packages/@mantine/dates/src/components/Month/get-start-of-week/get-start-of-week.ts +++ b/packages/@mantine/dates/src/components/Month/get-start-of-week/get-start-of-week.ts @@ -1,11 +1,11 @@ +import dayjs from 'dayjs'; import type { DayOfWeek } from '../../../types'; export function getStartOfWeek(date: Date, firstDayOfWeek: DayOfWeek = 1) { - const value = new Date(date); - - while (value.getDay() !== firstDayOfWeek) { - value.setDate(value.getDate() - 1); + let value = dayjs(date); + while (value.day() !== firstDayOfWeek) { + value = value.subtract(1, 'day'); } - return value; + return value.toDate(); } diff --git a/packages/@mantine/dates/src/components/Month/is-same-month/is-same-month.ts b/packages/@mantine/dates/src/components/Month/is-same-month/is-same-month.ts index 3391ec4ee99..02de6cc273b 100644 --- a/packages/@mantine/dates/src/components/Month/is-same-month/is-same-month.ts +++ b/packages/@mantine/dates/src/components/Month/is-same-month/is-same-month.ts @@ -1,5 +1,9 @@ +import dayjs from "dayjs"; + export function isSameMonth(date: Date, comparison: Date) { + const dayDate = dayjs(date) + const dayComparison = dayjs(comparison) return ( - date.getFullYear() === comparison.getFullYear() && date.getMonth() === comparison.getMonth() + dayDate.year() === dayComparison.year() && dayDate.month() === dayComparison.month() ); -} +} \ No newline at end of file