mirror of
https://github.com/ionic-team/ionic-framework.git
synced 2025-08-16 10:01:59 +08:00
fix(datetime): display time in user's timezone after selection (#25694)
Resolves #25693
This commit is contained in:
@ -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');
|
||||
});
|
||||
});
|
||||
|
@ -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,
|
||||
})
|
||||
)
|
||||
);
|
||||
};
|
||||
|
||||
/**
|
||||
|
Reference in New Issue
Block a user