Skip to content
14 changes: 11 additions & 3 deletions modules/angular2/src/common/pipes/date_pipe.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ import {
isDate,
isNumber,
isPresent,
isString,
Date,
DateWrapper,
CONST,
Expand Down Expand Up @@ -33,7 +34,7 @@ var defaultLocale: string = 'en-US';
*
* expression | date[:format]
*
* where `expression` is a date object or a number (milliseconds since UTC epoch) and
* where `expression` is a date object or a number (milliseconds since UTC epoch) or ISO string and
* `format` indicates which date/time components to include:
*
* | Component | Symbol | Short Form | Long Form | Numeric | 2-digit |
Expand Down Expand Up @@ -99,7 +100,9 @@ export class DatePipe implements PipeTransform {
'mediumTime': 'jms',
'shortTime': 'jm'
};

/** @internal */
static _ISO8601_STR: RegExp =
/(\d{4}-[01]\d-[0-3]\dT[0-2]\d:[0-5]\d:[0-5]\d\.\d+([+-][0-2]\d:[0-5]\d|Z))|(\d{4}-[01]\d-[0-3]\dT[0-2]\d:[0-5]\d:[0-5]\d([+-][0-2]\d:[0-5]\d|Z))|(\d{4}-[01]\d-[0-3]\dT[0-2]\d:[0-5]\d([+-][0-2]\d:[0-5]\d|Z))/g;

transform(value: any, args: any[]): string {
if (isBlank(value)) return null;
Expand All @@ -112,11 +115,16 @@ export class DatePipe implements PipeTransform {
if (isNumber(value)) {
value = DateWrapper.fromMillis(value);
}
if (DatePipe._ISO8601_STR.test(value)) {
value = DateWrapper.fromISOString(value);
}
if (StringMapWrapper.contains(DatePipe._ALIASES, pattern)) {
pattern = <string>StringMapWrapper.get(DatePipe._ALIASES, pattern);
}
return DateFormatter.format(value, defaultLocale, pattern);
}

supports(obj: any): boolean { return isDate(obj) || isNumber(obj); }
supports(obj: any): boolean {
return isDate(obj) || isNumber(obj) || (isString(obj) && DatePipe._ISO8601_STR.test(obj));
}
}
1 change: 1 addition & 0 deletions modules/angular2/test/common/pipes/date_pipe_spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@ export function main() {
describe("supports", () => {
it("should support date", () => { expect(pipe.supports(date)).toBe(true); });
it("should support int", () => { expect(pipe.supports(123456789)).toBe(true); });
it("should support date iso string", () => { expect(pipe.supports('2016-04-15T18:06:08-07:00')).toBe(true); });

it("should not support other objects", () => {
expect(pipe.supports(new Object())).toBe(false);
Expand Down