Fix date format for midday
In Chrome, when used locale ‘en-GB’ in Intl.DateTimeFormat for 12:00, it formats to 00:00 PM. The proper format should be 12:00 PM. Date-utils will use ‘en-GB’ only as a workaround for format ‘HH’ (24 hour), because Chrome 80 formats 00:05 as 24:05 in ‘en-US’. Change-Id: I6539315a6a798d580934b46937e050f48b1cf1c9
This commit is contained in:
@@ -108,8 +108,14 @@ export function formatDate(date, format) {
|
||||
if (format.includes('ss')) {
|
||||
options.second = '2-digit';
|
||||
}
|
||||
// en-GB is using h23 on Chrome 80, en-US is using h24 (midnight is 24:00)
|
||||
const dtf = new Intl.DateTimeFormat('en-GB', options);
|
||||
let locale = 'en-US';
|
||||
// Workaround for Chrome 80, en-US is using h24 (midnight is 24:00),
|
||||
// en-GB is using h23 (midnight is 00:00)
|
||||
if (format.includes('HH')) {
|
||||
locale = 'en-GB';
|
||||
}
|
||||
|
||||
const dtf = new Intl.DateTimeFormat(locale, options);
|
||||
const parts = dtf.formatToParts(date).filter(o => o.type != 'literal')
|
||||
.reduce((acc, o) => {
|
||||
acc[o.type] = o.value;
|
||||
|
@@ -110,5 +110,12 @@ suite('date-util tests', () => {
|
||||
formatDate(new Date('Jul 03 2013 17:00:00'), isoFormat + ' '
|
||||
+ timeFormat));
|
||||
});
|
||||
test('h:mm:ss A shows correctly midnight and midday', () => {
|
||||
const timeFormat = 'h:mm A';
|
||||
assert.equal('12:14 PM',
|
||||
formatDate(new Date('Jul 03 2013 12:14:00'), timeFormat));
|
||||
assert.equal('12:15 AM',
|
||||
formatDate(new Date('Jul 03 2013 00:15:00'), timeFormat));
|
||||
});
|
||||
});
|
||||
});
|
Reference in New Issue
Block a user