Skip to content

Commit

Permalink
feat(common): DatePipe supports ISO string
Browse files Browse the repository at this point in the history
Closes #7794
  • Loading branch information
laco0416 authored and mhevery committed May 23, 2016
1 parent 7d853dd commit abc266f
Show file tree
Hide file tree
Showing 2 changed files with 18 additions and 3 deletions.
18 changes: 15 additions & 3 deletions modules/@angular/common/src/pipes/date_pipe.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ import {PipeTransform, Pipe, Injectable} from '@angular/core';
import {
isDate,
isNumber,
isString,
DateWrapper,
isBlank,
} from '../../src/facade/lang';
Expand Down Expand Up @@ -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 |
* |-----------|:------:|--------------|-------------------|-----------|-----------|
Expand Down Expand Up @@ -105,12 +107,22 @@ 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 = <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 {
if (isDate(obj) || isNumber(obj)) {
return true;
}
if (isString(obj) && isDate(DateWrapper.fromISOString(obj))) {
return true;
}
return false;
}
}
3 changes: 3 additions & 0 deletions modules/@angular/common/test/pipes/date_pipe_spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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);
});
});

Expand Down

0 comments on commit abc266f

Please sign in to comment.