diff --git a/modules/@angular/common/src/pipes/date_pipe.ts b/modules/@angular/common/src/pipes/date_pipe.ts index 8e0f3d8376..29c5992967 100644 --- a/modules/@angular/common/src/pipes/date_pipe.ts +++ b/modules/@angular/common/src/pipes/date_pipe.ts @@ -2,6 +2,7 @@ import {PipeTransform, Pipe, Injectable} from '@angular/core'; import { isDate, isNumber, + isString, DateWrapper, isBlank, } from '../../src/facade/lang'; @@ -29,8 +30,9 @@ var defaultLocale: string = 'en-US'; * * expression | date[:format] * - * where `expression` is a date object or a number (milliseconds since UTC epoch) and - * `format` indicates which date/time components to include: + * where `expression` is a date object or a number (milliseconds since UTC epoch) or an ISO string + * (https://www.w3.org/TR/NOTE-datetime) and `format` indicates which date/time components to + * include: * * | Component | Symbol | Short Form | Long Form | Numeric | 2-digit | * |-----------|:------:|--------------|-------------------|-----------|-----------| @@ -105,6 +107,8 @@ export class DatePipe implements PipeTransform { if (isNumber(value)) { value = DateWrapper.fromMillis(value); + } else if (isString(value)) { + value = DateWrapper.fromISOString(value); } if (StringMapWrapper.contains(DatePipe._ALIASES, pattern)) { pattern = StringMapWrapper.get(DatePipe._ALIASES, pattern); @@ -112,5 +116,13 @@ export class DatePipe implements PipeTransform { return DateFormatter.format(value, defaultLocale, pattern); } - supports(obj: any): boolean { return isDate(obj) || isNumber(obj); } + supports(obj: any): boolean { + if (isDate(obj) || isNumber(obj)) { + return true; + } + if (isString(obj) && isDate(DateWrapper.fromISOString(obj))) { + return true; + } + return false; + } } diff --git a/modules/@angular/common/test/pipes/date_pipe_spec.ts b/modules/@angular/common/test/pipes/date_pipe_spec.ts index 80e5ca9260..fad0c0b2fd 100644 --- a/modules/@angular/common/test/pipes/date_pipe_spec.ts +++ b/modules/@angular/common/test/pipes/date_pipe_spec.ts @@ -30,10 +30,13 @@ 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 ISO string", + () => { expect(pipe.supports("2015-06-15T21:43:11Z")).toBe(true); }); it("should not support other objects", () => { expect(pipe.supports(new Object())).toBe(false); expect(pipe.supports(null)).toBe(false); + expect(pipe.supports("")).toBe(false); }); });