Skip to content

Commit

Permalink
Merge pull request #130 from DmitriySevkovych/fix/dates-shift-utc
Browse files Browse the repository at this point in the history
Fix: correct Calendar date display
  • Loading branch information
DmitriySevkovych authored Aug 15, 2024
2 parents c14da59 + fef08b6 commit 884494f
Show file tree
Hide file tree
Showing 2 changed files with 7 additions and 12 deletions.
14 changes: 7 additions & 7 deletions apps/frontend/src/components/Calendar.tsx
Original file line number Diff line number Diff line change
@@ -1,8 +1,4 @@
import {
formatDate,
formatDateToWords,
handleUnwantedTimezoneShift,
} from 'domain-model'
import { dateFromString, formatDate, formatDateToWords } from 'domain-model'
import { CalendarIcon } from 'lucide-react'
import React, { useState } from 'react'
import { ControllerRenderProps, UseFormReturn } from 'react-hook-form'
Expand Down Expand Up @@ -34,7 +30,7 @@ export function Calendar<T>(props: CalendarProps<T>) {
field: ControllerRenderProps<any, string>,
selectedDate: Date
) => {
field.onChange(handleUnwantedTimezoneShift(selectedDate))
field.onChange(dateFromString(selectedDate.toISOString()))
setIsOpen(false)
}

Expand Down Expand Up @@ -95,7 +91,11 @@ export const CalendarStandalone: React.FC<CalendarStandaloneProps> = ({
const [isOpen, setIsOpen] = useState<boolean>(false)

const handleSelect = (selectedDate: Date | undefined) => {
setValue(handleUnwantedTimezoneShift(selectedDate as Date))
setValue(
dateFromString(
selectedDate?.toISOString() || new Date().toISOString()
)
)
setIsOpen(false)
}

Expand Down
5 changes: 0 additions & 5 deletions packages/domain-model/src/models/dates.model.ts
Original file line number Diff line number Diff line change
Expand Up @@ -92,11 +92,6 @@ export const dateFromString = (dateString: string): Date => {
return date.toJSDate()
}

export const handleUnwantedTimezoneShift = (date: Date): Date => {
const userTimezoneOffset = date.getTimezoneOffset() * 60000
return new Date(date.getTime() + userTimezoneOffset)
}

export const getNumberOfDaysInMonth = (year: number, month: number): number => {
return new Date(year, month + 1, 0).getDate()
}
Expand Down

0 comments on commit 884494f

Please sign in to comment.