diff --git a/src/libs/DateUtils.ts b/src/libs/DateUtils.ts index 8fbfce4cf21c..1ba4ee4e460e 100644 --- a/src/libs/DateUtils.ts +++ b/src/libs/DateUtils.ts @@ -42,6 +42,7 @@ import { subMinutes, } from 'date-fns'; import {formatInTimeZone, fromZonedTime, toDate, toZonedTime, format as tzFormat} from 'date-fns-tz'; +import {enGB} from 'date-fns/locale/en-GB'; import throttle from 'lodash/throttle'; import {setCurrentDate} from './actions/CurrentDate'; @@ -463,8 +464,8 @@ function extractTime12Hour(dateTimeString: string, isFullFormat = false): string return ''; } const date = new Date(dateTimeString); - // get12HourTimeObjectFromDate parses this back into TimePicker state, whose period is compared against the English - // CONST.TIME_PERIOD, so the marker has to stay English. That leaves StatusClearAfterPage showing an English AM/PM. + // get12HourTimeObjectFromDate parses this back with the same default locale, so the format and the parse stay + // in step in every language. // eslint-disable-next-line rulesdir/require-locale-for-localized-date-format -- see above return format(date, isFullFormat ? 'hh:mm:ss.SSS a' : 'hh:mm a'); } @@ -581,7 +582,8 @@ const combineDateAndTime = (updatedTime: string, inputDateTime: string): string } } else if (updatedTime.includes(':')) { // it's in "hh:mm a" format - const tempTime = parse(updatedTime, 'hh:mm a', new Date()); + // The picker always submits English AM/PM markers, which the app's active date-fns locale may not parse. + const tempTime = parse(updatedTime, 'hh:mm a', new Date(), {locale: enGB}); if (isValid(tempTime)) { parsedTime = tempTime; } @@ -639,8 +641,7 @@ function get12HourTimeObjectFromDate(dateTime: string, isFullFormat = false): {h minute: format(parsedTime, 'mm'), seconds: isFullFormat ? format(parsedTime, 'ss') : '00', milliseconds: isFullFormat ? format(parsedTime, 'SSS') : '000', - // eslint-disable-next-line rulesdir/require-locale-for-localized-date-format -- this is TimePicker state compared against the English CONST.TIME_PERIOD, not text shown to the user. - period: format(parsedTime, 'a').toUpperCase(), + period: parsedTime.getHours() >= 12 ? CONST.TIME_PERIOD.PM : CONST.TIME_PERIOD.AM, }; } diff --git a/tests/unit/DateUtilsTest.ts b/tests/unit/DateUtilsTest.ts index be81192c0676..699bebcf7616 100644 --- a/tests/unit/DateUtilsTest.ts +++ b/tests/unit/DateUtilsTest.ts @@ -724,4 +724,26 @@ describe('DateUtils', () => { expect(DateUtils.getRemainingSecondsInWindow(Date.now() - 31 * 1000, windowMs)).toBe(0); }); }); + + describe('time picker helpers with a non-English date-fns locale', () => { + beforeEach(() => IntlStore.load(CONST.LOCALES.DE)); + + it('combineDateAndTime parses the picker-submitted English AM/PM value', () => { + expect(DateUtils.combineDateAndTime('02:00 PM', '2026-08-04')).toBe('2026-08-04 14:00:00'); + expect(DateUtils.combineDateAndTime('08:00 AM', '2026-08-04 00:00:00')).toBe('2026-08-04 08:00:00'); + }); + + it('get12HourTimeObjectFromDate returns the AM/PM period for a localized time string', () => { + const localizedNoon = DateUtils.extractTime12Hour('2026-08-04 12:00:00'); + expect(DateUtils.get12HourTimeObjectFromDate(localizedNoon).period).toBe(CONST.TIME_PERIOD.PM); + const localizedMorning = DateUtils.extractTime12Hour('2026-08-04 08:00:00'); + expect(DateUtils.get12HourTimeObjectFromDate(localizedMorning)).toEqual({hour: '08', minute: '00', seconds: '00', milliseconds: '000', period: CONST.TIME_PERIOD.AM}); + }); + + it('per diem start/end range built from picker values validates', () => { + const newStart = DateUtils.combineDateAndTime('08:00 AM', '2026-08-04'); + const newEnd = DateUtils.combineDateAndTime('02:00 PM', '2026-08-04'); + expect(DateUtils.isValidStartEndTimeRange({startTime: newStart, endTime: newEnd})).toBe(true); + }); + }); });