Skip to content

formatDate returns AM/PM for "B" pattern in locale "en" #36566

@rubenprins

Description

@rubenprins

🐞 bug report

Affected Package

The issue is caused by package @angular/common (locales/extra).

Is this a regression?

This does not appear to be a regression.

Description

If you try to use formatDate with the "B" placeholder ("at night", "in the morning", etc.), this does not work for locale "en" (and sublocales) between 21:00 and 06:00. Instead of getting "at night", you'll get "AM/PM".

This is caused by a misinterpretation of the time rule ['23:00', '06:00'] in @angular/common/locales/extra/en*.ts, as it will never match:

if (currentHours >= hoursFrom && currentMinutes >= minutesFrom &&
(currentHours < hoursTo ||
(currentHours === hoursTo && currentMinutes < minutesTo))) {
result = dayPeriods[index];
}

This will happen for any rule that straddles midnight (although a quick glance only shows English to be affected).

🔬 Minimal Reproduction

formatDate('2018-06-27T04:23:00', 'h B', 'en') + ', ' + formatDate('2018-06-27T06:23:00', 'h B', 'en')

Expected: 4 at night, 6 in the morning, actual: 4 AM, 6 in the morning.

Compared to Dutch: 4 's nachts, 6 's ochtends (not 4 a.m., 6 's ochtends).

🌍 Your Environment

Angular Version:
Angular 6–9

Metadata

Metadata

Labels

area: commonIssues related to APIs in the @angular/common packagearea: i18nIssues related to localization and internationalizationtype: bug/fix

Type

No type

Projects

No projects

Relationships

None yet

Development

No branches or pull requests

Issue actions