Skip to content

Commit de33a5e

Browse files
authored
fix: DatePicker displays correct day for configured timezone (#4622)
* fix: DatePicker displays correct day for configured timezone Pass the configured timezone to useDateFormatter in DateSegment so the date parts are formatted in the correct timezone instead of the browser's local timezone. Closes #4390 * fix: guard against empty string timezone in DatePicker and DateRangePicker * revert: undo overkill timezone validation guards
1 parent 2d1c353 commit de33a5e

2 files changed

Lines changed: 17 additions & 1 deletion

File tree

packages/eds-core-react/src/components/Datepicker/DatePicker.spec.tsx

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -297,6 +297,22 @@ describe('DatePicker', () => {
297297
expect(container.textContent).toMatch(/Verdien/)
298298
})
299299

300+
it('should display the date in the provided timezone, not the local timezone (bug #4390)', () => {
301+
// 2025-01-21 11:00 UTC = 2025-01-22 00:00 in Pacific/Auckland (NZDT, UTC+13)
302+
// Without the fix, the formatter uses browser/Node timezone (UTC) and displays Jan 21.
303+
const date = new Date('2025-01-21T11:00:00Z')
304+
render(
305+
<I18nProvider locale={'en-US'}>
306+
<DatePicker
307+
label={'Datepicker'}
308+
value={date}
309+
timezone={'Pacific/Auckland'}
310+
/>
311+
</I18nProvider>,
312+
)
313+
expect(screen.getByRole('presentation')).toHaveTextContent('01/22/2025')
314+
})
315+
300316
it('should display localized message for unavailable dates', () => {
301317
const unavailableDate = new Date(2024, 4, 30)
302318

packages/eds-core-react/src/components/Datepicker/fields/DateSegment.tsx

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -44,7 +44,7 @@ export function DateSegment({
4444
segment: IDateSegment
4545
}) {
4646
const { formatOptions, timezone } = useDatePickerContext()
47-
const formatter = useDateFormatter(formatOptions)
47+
const formatter = useDateFormatter({ ...formatOptions, timeZone: timezone })
4848
const parts = state.value
4949
? formatter.formatToParts(state.value.toDate(timezone))
5050
: []

0 commit comments

Comments
 (0)