Skip to content

Commit a397645

Browse files
committed
date filter should accept ISO 8601 formatted string as input
Closes angular#125
1 parent f077649 commit a397645

File tree

3 files changed

+17
-3
lines changed

3 files changed

+17
-3
lines changed

CHANGELOG.md

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,8 @@
11
# <angular/> 0.9.3 cold-resistance (in-progress) #
22

3+
### Api
4+
- date filter now accepts strings that angular.String.toDate can convert to Date objects
5+
36

47
# <angular/> 0.9.2 faunal-mimicry (2010-11-03) #
58

src/filters.js

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -168,7 +168,8 @@ var NUMBER_STRING = /^\d+$/;
168168
* * `'a'`: am/pm marker
169169
* * `'Z'`: 4 digit (+sign) representation of the timezone offset (-1200‒1200)
170170
*
171-
* @param {(Date|number|string)} date Date to format either as Date object or milliseconds.
171+
* @param {(Date|number|string)} date Date to format either as Date object, milliseconds (string or
172+
* number) or ISO 8601 string (yyyy-MM-ddTHH:mm:ssZ).
172173
* @param {string=} format Formatting rules. If not specified, Date#toLocaleDateString is used.
173174
* @returns {string} Formatted string or the input if input is not recognized as date/millis.
174175
*
@@ -188,8 +189,12 @@ var NUMBER_STRING = /^\d+$/;
188189
*
189190
*/
190191
angularFilter.date = function(date, format) {
191-
if (isString(date) && NUMBER_STRING.test(date)) {
192-
date = parseInt(date, 10);
192+
if (isString(date)) {
193+
if (NUMBER_STRING.test(date)) {
194+
date = parseInt(date, 10);
195+
} else {
196+
date = angularString.toDate(date);
197+
}
193198
}
194199

195200
if (isNumber(date)) {

test/FiltersSpec.js

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -128,5 +128,11 @@ describe('filter', function(){
128128
expect(filter.date(noon, "yyyy-MM-dd hh=HH:mm:ssaZ")).
129129
toEqual('2010-09-03 12=12:05:08pm0500');
130130
});
131+
132+
it('should be able to parse ISO 8601 dates/times using', function() {
133+
var isoString = '2010-09-03T05:05:08Z';
134+
expect(filter.date(isoString)).
135+
toEqual(angular.String.toDate(isoString).toLocaleDateString());
136+
});
131137
});
132138
});

0 commit comments

Comments
 (0)