Skip to content

Commit 11c69c8

Browse files
authored
fix(datetime): display time in user's timezone after selection (#25694)
Resolves #25693
1 parent 86b7000 commit 11c69c8

File tree

2 files changed

+66
-1
lines changed

2 files changed

+66
-1
lines changed

core/src/components/datetime/test/format.spec.ts

Lines changed: 57 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ import {
55
addTimePadding,
66
getMonthAndYear,
77
getLocalizedDayPeriod,
8+
getLocalizedTime,
89
} from '../utils/format';
910

1011
describe('generateDayAriaLabel()', () => {
@@ -99,3 +100,59 @@ describe('getLocalizedDayPeriod', () => {
99100
expect(getLocalizedDayPeriod('en-US', 'pm'));
100101
});
101102
});
103+
104+
describe('getLocalizedTime', () => {
105+
describe('with a timezone offset', () => {
106+
it('should ignore the offset and localize the time to PM', () => {
107+
const datetimeParts = {
108+
day: 1,
109+
month: 1,
110+
year: 2022,
111+
hour: 13,
112+
minute: 40,
113+
tzOffset: -240,
114+
};
115+
116+
expect(getLocalizedTime('en-US', datetimeParts, false)).toEqual('1:40 PM');
117+
});
118+
119+
it('should ignore the offset and localize the time to AM', () => {
120+
const datetimeParts = {
121+
day: 1,
122+
month: 1,
123+
year: 2022,
124+
hour: 9,
125+
minute: 40,
126+
tzOffset: -240,
127+
};
128+
129+
expect(getLocalizedTime('en-US', datetimeParts, false)).toEqual('9:40 AM');
130+
});
131+
});
132+
133+
it('should localize the time to PM', () => {
134+
const datetimeParts = {
135+
day: 1,
136+
month: 1,
137+
year: 2022,
138+
hour: 13,
139+
minute: 40,
140+
tzOffset: 0,
141+
};
142+
143+
expect(getLocalizedTime('en-US', datetimeParts, false)).toEqual('1:40 PM');
144+
});
145+
146+
it('should localize the time to AM', () => {
147+
const datetimeParts = {
148+
day: 1,
149+
month: 1,
150+
year: 2022,
151+
hour: 9,
152+
minute: 40,
153+
tzOffset: 0,
154+
};
155+
156+
expect(getLocalizedTime('en-US', datetimeParts, false)).toEqual('9:40 AM');
157+
});
158+
});

core/src/components/datetime/utils/format.ts

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,15 @@ export const getLocalizedTime = (locale: string, refParts: DatetimeParts, use24H
2020
minute: 'numeric',
2121
timeZone: 'UTC',
2222
hour12: !use24Hour,
23-
}).format(new Date(convertDataToISO(refParts)));
23+
}).format(
24+
new Date(
25+
convertDataToISO({
26+
...refParts,
27+
// TODO: FW-1831 will remove the need to manually set the tzOffset to undefined
28+
tzOffset: undefined,
29+
})
30+
)
31+
);
2432
};
2533

2634
/**

0 commit comments

Comments
 (0)