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

Commit 763b14f

Browse files
committed
feat(dateFilter): Add option to display date as am/pm
This adds a new option in order to choose whether to display a date as AM/PM or am/pm. Fix jshint error Fix regexp, update docs
1 parent cd21602 commit 763b14f

File tree

3 files changed

+30
-18
lines changed

3 files changed

+30
-18
lines changed

src/ng/filter/filters.js

+14-9
Original file line numberDiff line numberDiff line change
@@ -267,8 +267,11 @@ function weekGetter(size) {
267267
};
268268
}
269269

270-
function ampmGetter(date, formats) {
271-
return date.getHours() < 12 ? formats.AMPMS[0] : formats.AMPMS[1];
270+
function ampmGetter(capitalLetters) {
271+
var ampms = (capitalLetters) ? 'AMPMS' : 'ampms';
272+
return function(date, formats) {
273+
return date.getHours() < 12 ? formats[ampms][0] : formats[ampms][1];
274+
};
272275
}
273276

274277
var DATE_FORMATS = {
@@ -294,13 +297,14 @@ var DATE_FORMATS = {
294297
sss: dateGetter('Milliseconds', 3),
295298
EEEE: dateStrGetter('Day'),
296299
EEE: dateStrGetter('Day', true),
297-
a: ampmGetter,
300+
a: ampmGetter(false),
301+
A: ampmGetter(true),
298302
Z: timeZoneGetter,
299303
ww: weekGetter(2),
300304
w: weekGetter(1)
301305
};
302306

303-
var DATE_FORMATS_SPLIT = /((?:[^yMdHhmsaZEw']+)|(?:'(?:[^']|'')*')|(?:E+|y+|M+|d+|H+|h+|m+|s+|a|Z|w+))(.*)/,
307+
var DATE_FORMATS_SPLIT = /((?:[^yMdHhmsaAZEw']+)|(?:'(?:[^']|'')*')|(?:E+|y+|M+|d+|H+|h+|m+|s+|a|A|Z|w+))(.*)/,
304308
NUMBER_STRING = /^\-?\d+$/;
305309

306310
/**
@@ -333,24 +337,25 @@ var DATE_FORMATS_SPLIT = /((?:[^yMdHhmsaZEw']+)|(?:'(?:[^']|'')*')|(?:E+|y+|M+|d
333337
* * `'ss'`: Second in minute, padded (00-59)
334338
* * `'s'`: Second in minute (0-59)
335339
* * `'.sss' or ',sss'`: Millisecond in second, padded (000-999)
336-
* * `'a'`: am/pm marker
340+
* * `'a'`: am/pm marker, lowercase (am/pm)
341+
* * `'A'`: am/pm marker, uppercase (AM/PM)
337342
* * `'Z'`: 4 digit (+sign) representation of the timezone offset (-1200-+1200)
338343
* * `'ww'`: ISO-8601 week of year (00-53)
339344
* * `'w'`: ISO-8601 week of year (0-53)
340345
*
341346
* `format` string can also be one of the following predefined
342347
* {@link guide/i18n localizable formats}:
343348
*
344-
* * `'medium'`: equivalent to `'MMM d, y h:mm:ss a'` for en_US locale
349+
* * `'medium'`: equivalent to `'MMM d, y h:mm:ss A'` for en_US locale
345350
* (e.g. Sep 3, 2010 12:05:08 pm)
346-
* * `'short'`: equivalent to `'M/d/yy h:mm a'` for en_US locale (e.g. 9/3/10 12:05 pm)
351+
* * `'short'`: equivalent to `'M/d/yy h:mm A'` for en_US locale (e.g. 9/3/10 12:05 pm)
347352
* * `'fullDate'`: equivalent to `'EEEE, MMMM d, y'` for en_US locale
348353
* (e.g. Friday, September 3, 2010)
349354
* * `'longDate'`: equivalent to `'MMMM d, y'` for en_US locale (e.g. September 3, 2010)
350355
* * `'mediumDate'`: equivalent to `'MMM d, y'` for en_US locale (e.g. Sep 3, 2010)
351356
* * `'shortDate'`: equivalent to `'M/d/yy'` for en_US locale (e.g. 9/3/10)
352-
* * `'mediumTime'`: equivalent to `'h:mm:ss a'` for en_US locale (e.g. 12:05:08 pm)
353-
* * `'shortTime'`: equivalent to `'h:mm a'` for en_US locale (e.g. 12:05 pm)
357+
* * `'mediumTime'`: equivalent to `'h:mm:ss A'` for en_US locale (e.g. 12:05:08 pm)
358+
* * `'shortTime'`: equivalent to `'h:mm A'` for en_US locale (e.g. 12:05 pm)
354359
*
355360
* `format` string can contain literal values. These need to be escaped by surrounding with single quotes (e.g.
356361
* `"h 'in the morning'"`). In order to output a single quote, escape it - i.e., two single quotes in a sequence

src/ng/locale.js

+5-4
Original file line numberDiff line numberDiff line change
@@ -52,14 +52,15 @@ function $LocaleProvider(){
5252
DAY: 'Sunday,Monday,Tuesday,Wednesday,Thursday,Friday,Saturday'.split(','),
5353
SHORTDAY: 'Sun,Mon,Tue,Wed,Thu,Fri,Sat'.split(','),
5454
AMPMS: ['AM','PM'],
55-
medium: 'MMM d, y h:mm:ss a',
56-
short: 'M/d/yy h:mm a',
55+
ampms: ['am', 'pm'],
56+
medium: 'MMM d, y h:mm:ss A',
57+
short: 'M/d/yy h:mm A',
5758
fullDate: 'EEEE, MMMM d, y',
5859
longDate: 'MMMM d, y',
5960
mediumDate: 'MMM d, y',
6061
shortDate: 'M/d/yy',
61-
mediumTime: 'h:mm:ss a',
62-
shortTime: 'h:mm a'
62+
mediumTime: 'h:mm:ss A',
63+
shortTime: 'h:mm A'
6364
},
6465

6566
pluralCat: function(num) {

test/ng/filter/filtersSpec.js

+11-5
Original file line numberDiff line numberDiff line change
@@ -244,18 +244,24 @@ describe('filters', function() {
244244
toEqual('10-09-03 07:05:08.001');
245245

246246
expect(date(midnight, "yyyy-M-d h=H:m:saZ")).
247-
toEqual('2010-9-3 12=0:5:8AM-0500');
247+
toEqual('2010-9-3 12=0:5:8am-0500');
248248

249249
expect(date(midnight, "yyyy-MM-dd hh=HH:mm:ssaZ")).
250-
toEqual('2010-09-03 12=00:05:08AM-0500');
250+
toEqual('2010-09-03 12=00:05:08am-0500');
251251

252252
expect(date(midnight, "yyyy-MM-dd hh=HH:mm:ss.sssaZ")).
253+
toEqual('2010-09-03 12=00:05:08.123am-0500');
254+
255+
expect(date(midnight, "yyyy-MM-dd hh=HH:mm:ss.sssAZ")).
253256
toEqual('2010-09-03 12=00:05:08.123AM-0500');
254257

255258
expect(date(noon, "yyyy-MM-dd hh=HH:mm:ssaZ")).
256-
toEqual('2010-09-03 12=12:05:08PM-0500');
259+
toEqual('2010-09-03 12=12:05:08pm-0500');
257260

258261
expect(date(noon, "yyyy-MM-dd hh=HH:mm:ss.sssaZ")).
262+
toEqual('2010-09-03 12=12:05:08.012pm-0500');
263+
264+
expect(date(noon, "yyyy-MM-dd hh=HH:mm:ss.sssAZ")).
259265
toEqual('2010-09-03 12=12:05:08.012PM-0500');
260266

261267
expect(date(noon, "EEE, MMM d, yyyy")).
@@ -301,12 +307,12 @@ describe('filters', function() {
301307

302308
it('should treat single quoted strings as string literals', function() {
303309
expect(date(midnight, "yyyy'de' 'a'x'dd' 'adZ' h=H:m:saZ")).
304-
toEqual('2010de axdd adZ 12=0:5:8AM-0500');
310+
toEqual('2010de axdd adZ 12=0:5:8am-0500');
305311
});
306312

307313
it('should treat a sequence of two single quotes as a literal single quote', function() {
308314
expect(date(midnight, "yyyy'de' 'a''dd' 'adZ' h=H:m:saZ")).
309-
toEqual("2010de a'dd adZ 12=0:5:8AM-0500");
315+
toEqual("2010de a'dd adZ 12=0:5:8am-0500");
310316
});
311317

312318
it('should accept default formats', function() {

0 commit comments

Comments
 (0)