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

Commit 9ab8e2e

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
1 parent cd21602 commit 9ab8e2e

File tree

3 files changed

+25
-13
lines changed

3 files changed

+25
-13
lines changed

src/ng/filter/filters.js

+9-4
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,7 +297,8 @@ 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)
@@ -333,7 +337,8 @@ 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)

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)