Skip to content
This repository was archived by the owner on Apr 12, 2024. It is now read-only.

Commit 2b4dfa9

Browse files
fix(date filter): display localised era for G format codes
This implementation is limited to displaying only AD (CE) years correctly, since we do not support the `u` style year format that can be used to represent dates before 1 AD. Closes #10503 Closes #11266
1 parent 68dbbfb commit 2b4dfa9

File tree

2 files changed

+28
-2
lines changed

2 files changed

+28
-2
lines changed

src/ng/filter/filters.js

+16-2
Original file line numberDiff line numberDiff line change
@@ -302,6 +302,14 @@ function ampmGetter(date, formats) {
302302
return date.getHours() < 12 ? formats.AMPMS[0] : formats.AMPMS[1];
303303
}
304304

305+
function eraGetter(date, formats) {
306+
return date.getFullYear() <= 0 ? formats.ERAS[0] : formats.ERAS[1];
307+
}
308+
309+
function longEraGetter(date, formats) {
310+
return date.getFullYear() <= 0 ? formats.ERANAMES[0] : formats.ERANAMES[1];
311+
}
312+
305313
var DATE_FORMATS = {
306314
yyyy: dateGetter('FullYear', 4),
307315
yy: dateGetter('FullYear', 2, 0, true),
@@ -328,10 +336,14 @@ var DATE_FORMATS = {
328336
a: ampmGetter,
329337
Z: timeZoneGetter,
330338
ww: weekGetter(2),
331-
w: weekGetter(1)
339+
w: weekGetter(1),
340+
G: eraGetter,
341+
GG: eraGetter,
342+
GGG: eraGetter,
343+
GGGG: longEraGetter
332344
};
333345

334-
var DATE_FORMATS_SPLIT = /((?:[^yMdHhmsaZEw']+)|(?:'(?:[^']|'')*')|(?:E+|y+|M+|d+|H+|h+|m+|s+|a|Z|w+))(.*)/,
346+
var DATE_FORMATS_SPLIT = /((?:[^yMdHhmsaZEwG']+)|(?:'(?:[^']|'')*')|(?:E+|y+|M+|d+|H+|h+|m+|s+|a|Z|G+|w+))(.*)/,
335347
NUMBER_STRING = /^\-?\d+$/;
336348

337349
/**
@@ -368,6 +380,8 @@ var DATE_FORMATS_SPLIT = /((?:[^yMdHhmsaZEw']+)|(?:'(?:[^']|'')*')|(?:E+|y+|M+|d
368380
* * `'Z'`: 4 digit (+sign) representation of the timezone offset (-1200-+1200)
369381
* * `'ww'`: Week of year, padded (00-53). Week 01 is the week with the first Thursday of the year
370382
* * `'w'`: Week of year (0-53). Week 1 is the week with the first Thursday of the year
383+
* * `'G'`, `'GG'`, `'GGG'`: The abbreviated form of the era string (e.g. 'AD')
384+
* * `'GGGG'`: The long form of the era string (e.g. 'Anno Domini')
371385
*
372386
* `format` string can also be one of the following predefined
373387
* {@link guide/i18n localizable formats}:

test/ng/filter/filtersSpec.js

+12
Original file line numberDiff line numberDiff line change
@@ -298,6 +298,18 @@ describe('filters', function() {
298298

299299
expect(date(earlyDate, "MMMM dd, y")).
300300
toEqual('September 03, 1');
301+
302+
expect(date(noon, "MMMM dd, y G")).
303+
toEqual('September 03, 2010 AD');
304+
305+
expect(date(noon, "MMMM dd, y GG")).
306+
toEqual('September 03, 2010 AD');
307+
308+
expect(date(noon, "MMMM dd, y GGG")).
309+
toEqual('September 03, 2010 AD');
310+
311+
expect(date(noon, "MMMM dd, y GGGG")).
312+
toEqual('September 03, 2010 Anno Domini');
301313
});
302314

303315
it('should accept negative numbers as strings', function() {

0 commit comments

Comments
 (0)