|
| 1 | +import { |
| 2 | + isDate, |
| 3 | + isNumber, |
| 4 | + isPresent, |
| 5 | + Date, |
| 6 | + DateWrapper, |
| 7 | + CONST, |
| 8 | + FunctionWrapper |
| 9 | +} from 'angular2/src/facade/lang'; |
| 10 | +import {DateFormatter} from 'angular2/src/facade/intl'; |
| 11 | +import {StringMapWrapper, ListWrapper} from 'angular2/src/facade/collection'; |
| 12 | +import {Pipe, BasePipe, PipeFactory} from './pipe'; |
| 13 | +import {ChangeDetectorRef} from '../change_detector_ref'; |
| 14 | + |
| 15 | +// TODO: move to a global configable location along with other i18n components. |
| 16 | +var defaultLocale: string = 'en-US'; |
| 17 | + |
| 18 | +/** |
| 19 | + * Formats a date value to a string based on the requested format. |
| 20 | + * |
| 21 | + * # Usage |
| 22 | + * |
| 23 | + * expression | date[:format] |
| 24 | + * |
| 25 | + * where `expression` is a date object or a number (milliseconds since UTC epoch) and |
| 26 | + * `format` indicates which date/time components to include: |
| 27 | + * |
| 28 | + * | Component | Symbol | Short Form | Long Form | Numeric | 2-digit | |
| 29 | + * |-----------|:------:|--------------|-------------------|-----------|-----------| |
| 30 | + * | era | G | G (AD) | GGGG (Anno Domini)| - | - | |
| 31 | + * | year | y | - | - | y (2015) | yy (15) | |
| 32 | + * | month | M | MMM (Sep) | MMMM (September) | M (9) | MM (09) | |
| 33 | + * | day | d | - | - | d (3) | dd (03) | |
| 34 | + * | weekday | E | EEE (Sun) | EEEE (Sunday) | - | - | |
| 35 | + * | hour | j | - | - | j (13) | jj (13) | |
| 36 | + * | hour12 | h | - | - | h (1 PM) | hh (01 PM)| |
| 37 | + * | hour24 | H | - | - | H (13) | HH (13) | |
| 38 | + * | minute | m | - | - | m (5) | mm (05) | |
| 39 | + * | second | s | - | - | s (9) | ss (09) | |
| 40 | + * | timezone | z | - | z (Pacific Standard Time)| - | - | |
| 41 | + * | timezone | Z | Z (GMT-8:00) | - | - | - | |
| 42 | + * |
| 43 | + * In javascript, only the components specified will be respected (not the ordering, |
| 44 | + * punctuations, ...) and details of the the formatting will be dependent on the locale. |
| 45 | + * On the other hand in Dart version, you can also include quoted text as well as some extra |
| 46 | + * date/time components such as quarter. For more information see: |
| 47 | + * https://api.dartlang.org/apidocs/channels/stable/dartdoc-viewer/intl/intl.DateFormat. |
| 48 | + * |
| 49 | + * `format` can also be one of the following predefined formats: |
| 50 | + * |
| 51 | + * - `'medium'`: equivalent to `'yMMMdjms'` (e.g. Sep 3, 2010, 12:05:08 PM for en-US) |
| 52 | + * - `'short'`: equivalent to `'yMdjm'` (e.g. 9/3/2010, 12:05 PM for en-US) |
| 53 | + * - `'fullDate'`: equivalent to `'yMMMMEEEEd'` (e.g. Friday, September 3, 2010 for en-US) |
| 54 | + * - `'longDate'`: equivalent to `'yMMMMd'` (e.g. September 3, 2010) |
| 55 | + * - `'mediumDate'`: equivalent to `'yMMMd'` (e.g. Sep 3, 2010 for en-US) |
| 56 | + * - `'shortDate'`: equivalent to `'yMd'` (e.g. 9/3/2010 for en-US) |
| 57 | + * - `'mediumTime'`: equivalent to `'jms'` (e.g. 12:05:08 PM for en-US) |
| 58 | + * - `'shortTime'`: equivalent to `'jm'` (e.g. 12:05 PM for en-US) |
| 59 | + * |
| 60 | + * Timezone of the formatted text will be the local system timezone of the end-users machine. |
| 61 | + * |
| 62 | + * # Examples |
| 63 | + * |
| 64 | + * Assuming `dateObj` is (year: 2015, month: 6, day: 15, hour: 21, minute: 43, second: 11) |
| 65 | + * in the _local_ time and locale is 'en-US': |
| 66 | + * |
| 67 | + * {{ dateObj | date }} // output is 'Jun 15, 2015' |
| 68 | + * {{ dateObj | date:'medium' }} // output is 'Jun 15, 2015, 9:43:11 PM' |
| 69 | + * {{ dateObj | date:'shortTime' }} // output is '9:43 PM' |
| 70 | + * {{ dateObj | date:'mmss' }} // output is '43:11' |
| 71 | + * |
| 72 | + * @exportedAs angular2/pipes |
| 73 | + */ |
| 74 | +@CONST() |
| 75 | +export class DatePipe extends BasePipe implements PipeFactory { |
| 76 | + static _ALIASES = { |
| 77 | + 'medium': 'yMMMdjms', |
| 78 | + 'short': 'yMdjm', |
| 79 | + 'fullDate': 'yMMMMEEEEd', |
| 80 | + 'longDate': 'yMMMMd', |
| 81 | + 'mediumDate': 'yMMMd', |
| 82 | + 'shortDate': 'yMd', |
| 83 | + 'mediumTime': 'jms', |
| 84 | + 'shortTime': 'jm' |
| 85 | + }; |
| 86 | + |
| 87 | + |
| 88 | + transform(value, args: List<any>): string { |
| 89 | + var pattern: string = isPresent(args) && args.length > 0 ? args[0] : 'mediumDate'; |
| 90 | + if (isNumber(value)) { |
| 91 | + value = DateWrapper.fromMillis(value); |
| 92 | + } |
| 93 | + if (StringMapWrapper.contains(DatePipe._ALIASES, pattern)) { |
| 94 | + pattern = <string>StringMapWrapper.get(DatePipe._ALIASES, pattern); |
| 95 | + } |
| 96 | + return DateFormatter.format(value, defaultLocale, pattern); |
| 97 | + } |
| 98 | + |
| 99 | + supports(obj): boolean { return isDate(obj) || isNumber(obj); } |
| 100 | + |
| 101 | + create(cdRef: ChangeDetectorRef): Pipe { return this } |
| 102 | +} |
0 commit comments