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

Commit

Permalink
feat(dateFilter): Add option to display date as am/pm
Browse files Browse the repository at this point in the history
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
  • Loading branch information
wesleycho committed Aug 29, 2014
1 parent cd21602 commit 763b14f
Show file tree
Hide file tree
Showing 3 changed files with 30 additions and 18 deletions.
23 changes: 14 additions & 9 deletions src/ng/filter/filters.js
Original file line number Diff line number Diff line change
Expand Up @@ -267,8 +267,11 @@ function weekGetter(size) {
};
}

function ampmGetter(date, formats) {
return date.getHours() < 12 ? formats.AMPMS[0] : formats.AMPMS[1];
function ampmGetter(capitalLetters) {
var ampms = (capitalLetters) ? 'AMPMS' : 'ampms';
return function(date, formats) {
return date.getHours() < 12 ? formats[ampms][0] : formats[ampms][1];
};
}

var DATE_FORMATS = {
Expand All @@ -294,13 +297,14 @@ var DATE_FORMATS = {
sss: dateGetter('Milliseconds', 3),
EEEE: dateStrGetter('Day'),
EEE: dateStrGetter('Day', true),
a: ampmGetter,
a: ampmGetter(false),
A: ampmGetter(true),
Z: timeZoneGetter,
ww: weekGetter(2),
w: weekGetter(1)
};

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

/**
Expand Down Expand Up @@ -333,24 +337,25 @@ var DATE_FORMATS_SPLIT = /((?:[^yMdHhmsaZEw']+)|(?:'(?:[^']|'')*')|(?:E+|y+|M+|d
* * `'ss'`: Second in minute, padded (00-59)
* * `'s'`: Second in minute (0-59)
* * `'.sss' or ',sss'`: Millisecond in second, padded (000-999)
* * `'a'`: am/pm marker
* * `'a'`: am/pm marker, lowercase (am/pm)
* * `'A'`: am/pm marker, uppercase (AM/PM)
* * `'Z'`: 4 digit (+sign) representation of the timezone offset (-1200-+1200)
* * `'ww'`: ISO-8601 week of year (00-53)
* * `'w'`: ISO-8601 week of year (0-53)
*
* `format` string can also be one of the following predefined
* {@link guide/i18n localizable formats}:
*
* * `'medium'`: equivalent to `'MMM d, y h:mm:ss a'` for en_US locale
* * `'medium'`: equivalent to `'MMM d, y h:mm:ss A'` for en_US locale
* (e.g. Sep 3, 2010 12:05:08 pm)
* * `'short'`: equivalent to `'M/d/yy h:mm a'` for en_US locale (e.g. 9/3/10 12:05 pm)
* * `'short'`: equivalent to `'M/d/yy h:mm A'` for en_US locale (e.g. 9/3/10 12:05 pm)
* * `'fullDate'`: equivalent to `'EEEE, MMMM d, y'` for en_US locale
* (e.g. Friday, September 3, 2010)
* * `'longDate'`: equivalent to `'MMMM d, y'` for en_US locale (e.g. September 3, 2010)
* * `'mediumDate'`: equivalent to `'MMM d, y'` for en_US locale (e.g. Sep 3, 2010)
* * `'shortDate'`: equivalent to `'M/d/yy'` for en_US locale (e.g. 9/3/10)
* * `'mediumTime'`: equivalent to `'h:mm:ss a'` for en_US locale (e.g. 12:05:08 pm)
* * `'shortTime'`: equivalent to `'h:mm a'` for en_US locale (e.g. 12:05 pm)
* * `'mediumTime'`: equivalent to `'h:mm:ss A'` for en_US locale (e.g. 12:05:08 pm)
* * `'shortTime'`: equivalent to `'h:mm A'` for en_US locale (e.g. 12:05 pm)
*
* `format` string can contain literal values. These need to be escaped by surrounding with single quotes (e.g.
* `"h 'in the morning'"`). In order to output a single quote, escape it - i.e., two single quotes in a sequence
Expand Down
9 changes: 5 additions & 4 deletions src/ng/locale.js
Original file line number Diff line number Diff line change
Expand Up @@ -52,14 +52,15 @@ function $LocaleProvider(){
DAY: 'Sunday,Monday,Tuesday,Wednesday,Thursday,Friday,Saturday'.split(','),
SHORTDAY: 'Sun,Mon,Tue,Wed,Thu,Fri,Sat'.split(','),
AMPMS: ['AM','PM'],
medium: 'MMM d, y h:mm:ss a',
short: 'M/d/yy h:mm a',
ampms: ['am', 'pm'],
medium: 'MMM d, y h:mm:ss A',
short: 'M/d/yy h:mm A',
fullDate: 'EEEE, MMMM d, y',
longDate: 'MMMM d, y',
mediumDate: 'MMM d, y',
shortDate: 'M/d/yy',
mediumTime: 'h:mm:ss a',
shortTime: 'h:mm a'
mediumTime: 'h:mm:ss A',
shortTime: 'h:mm A'
},

pluralCat: function(num) {
Expand Down
16 changes: 11 additions & 5 deletions test/ng/filter/filtersSpec.js
Original file line number Diff line number Diff line change
Expand Up @@ -244,18 +244,24 @@ describe('filters', function() {
toEqual('10-09-03 07:05:08.001');

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

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

expect(date(midnight, "yyyy-MM-dd hh=HH:mm:ss.sssaZ")).
toEqual('2010-09-03 12=00:05:08.123am-0500');

expect(date(midnight, "yyyy-MM-dd hh=HH:mm:ss.sssAZ")).
toEqual('2010-09-03 12=00:05:08.123AM-0500');

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

expect(date(noon, "yyyy-MM-dd hh=HH:mm:ss.sssaZ")).
toEqual('2010-09-03 12=12:05:08.012pm-0500');

expect(date(noon, "yyyy-MM-dd hh=HH:mm:ss.sssAZ")).
toEqual('2010-09-03 12=12:05:08.012PM-0500');

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

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

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

it('should accept default formats', function() {
Expand Down

0 comments on commit 763b14f

Please sign in to comment.