diff --git a/core/src/components/datetime/test/format.spec.ts b/core/src/components/datetime/test/format.spec.ts index 23676b44de..ef9fb4ea2f 100644 --- a/core/src/components/datetime/test/format.spec.ts +++ b/core/src/components/datetime/test/format.spec.ts @@ -5,6 +5,7 @@ import { addTimePadding, getMonthAndYear, getLocalizedDayPeriod, + getLocalizedTime, } from '../utils/format'; describe('generateDayAriaLabel()', () => { @@ -99,3 +100,59 @@ describe('getLocalizedDayPeriod', () => { expect(getLocalizedDayPeriod('en-US', 'pm')); }); }); + +describe('getLocalizedTime', () => { + describe('with a timezone offset', () => { + it('should ignore the offset and localize the time to PM', () => { + const datetimeParts = { + day: 1, + month: 1, + year: 2022, + hour: 13, + minute: 40, + tzOffset: -240, + }; + + expect(getLocalizedTime('en-US', datetimeParts, false)).toEqual('1:40 PM'); + }); + + it('should ignore the offset and localize the time to AM', () => { + const datetimeParts = { + day: 1, + month: 1, + year: 2022, + hour: 9, + minute: 40, + tzOffset: -240, + }; + + expect(getLocalizedTime('en-US', datetimeParts, false)).toEqual('9:40 AM'); + }); + }); + + it('should localize the time to PM', () => { + const datetimeParts = { + day: 1, + month: 1, + year: 2022, + hour: 13, + minute: 40, + tzOffset: 0, + }; + + expect(getLocalizedTime('en-US', datetimeParts, false)).toEqual('1:40 PM'); + }); + + it('should localize the time to AM', () => { + const datetimeParts = { + day: 1, + month: 1, + year: 2022, + hour: 9, + minute: 40, + tzOffset: 0, + }; + + expect(getLocalizedTime('en-US', datetimeParts, false)).toEqual('9:40 AM'); + }); +}); diff --git a/core/src/components/datetime/utils/format.ts b/core/src/components/datetime/utils/format.ts index 64747f770e..3869d376b4 100644 --- a/core/src/components/datetime/utils/format.ts +++ b/core/src/components/datetime/utils/format.ts @@ -20,7 +20,15 @@ export const getLocalizedTime = (locale: string, refParts: DatetimeParts, use24H minute: 'numeric', timeZone: 'UTC', hour12: !use24Hour, - }).format(new Date(convertDataToISO(refParts))); + }).format( + new Date( + convertDataToISO({ + ...refParts, + // TODO: FW-1831 will remove the need to manually set the tzOffset to undefined + tzOffset: undefined, + }) + ) + ); }; /**