1 | # angular2-moment
|
2 |
|
3 | moment.js pipes for Angular
|
4 |
|
5 | [![Build Status](https://travis-ci.org/urish/angular2-moment.png?branch=master)](https://travis-ci.org/urish/angular2-moment)
|
6 |
|
7 | This module works with Angular 2.0 and above.
|
8 |
|
9 | For the AngularJS version of this module, please see [angular-moment](https://github.com/urish/angular-moment).
|
10 |
|
11 | Installation
|
12 | ------------
|
13 |
|
14 | `npm install --save angular2-moment`
|
15 |
|
16 | If you use typescript 1.8, and [typings](https://github.com/typings/typings), you may also need to install typings for moment.js:
|
17 |
|
18 | `typings install --save moment`
|
19 |
|
20 | ### For System.js users:
|
21 |
|
22 | First you need to install moment:
|
23 |
|
24 | `npm install moment --save`
|
25 |
|
26 | Don't forget to update your systemjs.config.js:
|
27 |
|
28 | ```
|
29 | packages: {
|
30 | app: {
|
31 | main: './main.js',
|
32 | defaultExtension: 'js'
|
33 | },
|
34 | 'moment': {
|
35 | main: './moment.js',
|
36 | defaultExtension: 'js'
|
37 | },
|
38 | 'angular2-moment': {
|
39 | main: './index.js',
|
40 | defaultExtension: 'js'
|
41 | }
|
42 | }
|
43 | ```
|
44 |
|
45 | Usage
|
46 | -----
|
47 |
|
48 | Import `MomentModule` into your app's modules:
|
49 |
|
50 | ``` typescript
|
51 | import { MomentModule } from 'angular2-moment';
|
52 |
|
53 | @NgModule({
|
54 | imports: [
|
55 | MomentModule
|
56 | ]
|
57 | })
|
58 | ```
|
59 |
|
60 | This makes all the `angular2-moment` pipes available for use in your app components.
|
61 |
|
62 | Available pipes
|
63 | ---------------
|
64 |
|
65 | ## amTimeAgo pipe
|
66 | Takes an optional `omitSuffix` argument that defaults to `false`.
|
67 |
|
68 | ``` typescript
|
69 | @Component({
|
70 | selector: 'app',
|
71 | template: `
|
72 | Last updated: {{myDate | amTimeAgo}}
|
73 | `
|
74 | })
|
75 | ```
|
76 |
|
77 | Prints `Last updated: a few seconds ago`
|
78 |
|
79 | ``` typescript
|
80 | @Component({
|
81 | selector: 'app',
|
82 | template: `
|
83 | Last updated: {{myDate | amTimeAgo:true}}
|
84 | `
|
85 | })
|
86 | ```
|
87 |
|
88 | Prints `Last updated: a few seconds`
|
89 |
|
90 | ## amCalendar pipe
|
91 | Takes optional `referenceTime` argument (defaults to now)
|
92 | and `formats` argument that could be output formats object or callback function.
|
93 | See [momentjs docs](http://momentjs.com/docs/#/displaying/calendar-time/) for details.
|
94 |
|
95 | ``` typescript
|
96 | @Component({
|
97 | selector: 'app',
|
98 | template: `
|
99 | Last updated: {{myDate | amCalendar}}
|
100 | `
|
101 | })
|
102 | ```
|
103 |
|
104 | Prints `Last updated: Today at 14:00` (default referenceTime is today by default)
|
105 |
|
106 | ``` typescript
|
107 | @Component({
|
108 | selector: 'app',
|
109 | template: `
|
110 | Last updated: <time>{{myDate | amCalendar:nextDay }}</time>
|
111 | `
|
112 | })
|
113 | export class AppComponent {
|
114 | nextDay: Date;
|
115 |
|
116 | constructor() {
|
117 | this.nextDay = new Date();
|
118 | nextDay.setDate(nextDay.getDate() + 1);
|
119 | }
|
120 | }
|
121 | ```
|
122 |
|
123 | Prints `Last updated: Yesterday at 14:00` (referenceTime is tomorrow)
|
124 |
|
125 | ``` typescript
|
126 | @Component({
|
127 | selector: 'app',
|
128 | template: `
|
129 | Last updated: <time>{{myDate | amCalendar:{sameDay:'[Same Day at] h:mm A'} }}</time>
|
130 | `
|
131 | })
|
132 | ```
|
133 |
|
134 | Prints `Last updated: Same Day at 2:00 PM`
|
135 |
|
136 | ## amDateFormat pipe
|
137 |
|
138 | ``` typescript
|
139 | @Component({
|
140 | selector: 'app',
|
141 | template: `
|
142 | Last updated: {{myDate | amDateFormat:'LL'}}
|
143 | `
|
144 | })
|
145 | ```
|
146 |
|
147 | Prints `Last updated: January 24, 2016`
|
148 |
|
149 | ## amParse pipe
|
150 |
|
151 | Parses a custom-formatted date into a moment object that can be used with the other pipes.
|
152 |
|
153 | ``` typescript
|
154 | @Component({
|
155 | selector: 'app',
|
156 | template: `
|
157 | Last updated: {{'24/01/2014' | amParse:'DD/MM/YYYY' | amDateFormat:'LL'}}
|
158 | `
|
159 | })
|
160 | ```
|
161 |
|
162 | Prints `Last updated: January 24, 2016`
|
163 |
|
164 | ## amLocal pipe
|
165 |
|
166 | Converts UTC time to local time.
|
167 |
|
168 | ``` typescript
|
169 | @Component({
|
170 | selector: 'app',
|
171 | template: `
|
172 | Last updated: {{mydate | amLocal | amDateFormat: 'YYYY-MM-DD HH:mm'}}
|
173 | `
|
174 | })
|
175 | ```
|
176 |
|
177 | Prints `Last updated 2016-01-24 12:34`
|
178 |
|
179 | ## amLocale pipe
|
180 |
|
181 | To be used with amDateFormat pipe in order to change locale.
|
182 |
|
183 | ``` typescript
|
184 | @Component({
|
185 | selector: 'app',
|
186 | template: `
|
187 | Last updated: {{'2016-01-24 14:23:45' | amLocale:'en' | amDateFormat:'MMMM Do YYYY, h:mm:ss a'}}
|
188 | `
|
189 | })
|
190 | ```
|
191 |
|
192 | Prints `Last updated: January 24th 2016, 2:23:45 pm`
|
193 |
|
194 | ## amFromUnix pipe
|
195 |
|
196 | ``` typescript
|
197 | @Component({
|
198 | selector: 'app',
|
199 | template: `
|
200 | Last updated: {{ (1456263980 | amFromUnix) | amDateFormat:'hh:mmA'}}
|
201 | `
|
202 | })
|
203 | ```
|
204 |
|
205 | Prints `Last updated: 01:46PM`
|
206 |
|
207 | ## amDuration pipe
|
208 |
|
209 | ``` typescript
|
210 | @Component({
|
211 | selector: 'app',
|
212 | template: `
|
213 | Uptime: {{ 365 | amDuration:'seconds' }}
|
214 | `
|
215 | })
|
216 | ```
|
217 |
|
218 | Prints `Uptime: 6 minutes`
|
219 |
|
220 | ## amDifference pipe
|
221 |
|
222 | ``` typescript
|
223 | @Component({
|
224 | selector: 'app',
|
225 | template: `
|
226 | Expiration: {{nextDay | amDifference: today :'days' : true}} days
|
227 | `
|
228 | })
|
229 | ```
|
230 | Prints `Expiration: 1 day`
|
231 |
|
232 | ## amAdd and amSubtract pipes
|
233 |
|
234 | Use these pipes to perform date arithmetics. See [momentjs docs](http://momentjs.com/docs/#/manipulating/add/) for details.
|
235 |
|
236 | ``` typescript
|
237 | @Component({
|
238 | selector: 'app',
|
239 | template: `
|
240 | Expiration: {{'2017-03-17T16:55:00.000+01:00' | amAdd: 2 : 'hours' | amDateFormat: 'YYYY-MM-DD HH:mm'}}
|
241 | `
|
242 | })
|
243 | ```
|
244 | Prints `Expiration: 2017-03-17 18:55`
|
245 |
|
246 | ``` typescript
|
247 | @Component({
|
248 | selector: 'app',
|
249 | template: `
|
250 | Last updated: {{'2017-03-17T16:55:00.000+01:00' | amSubtract: 5 : 'years' | amDateFormat: 'YYYY-MM-DD HH:mm'}}
|
251 | `
|
252 | })
|
253 | ```
|
254 | Prints `Last updated: 2012-03-17 16:55`
|
255 |
|
256 | ## amFromUtc pipe
|
257 |
|
258 | Parses the date as UTC and enables mode for subsequent moment operations (such as displaying the time in UTC). This can be combined with `amLocal` to display a UTC date in local time.
|
259 |
|
260 | ``` typescript
|
261 | @Component({
|
262 | selector: 'app',
|
263 | template: `
|
264 | Last updated: {{ '2016-12-31T23:00:00.000-01:00' | amFromUtc | amDateFormat: 'YYYY-MM-DD' }}
|
265 | `
|
266 | })
|
267 | ```
|
268 |
|
269 | Prints `Last updated: 2017-01-01`
|
270 |
|
271 | ## amUtc pipe
|
272 |
|
273 | Enables UTC mode for subsequent moment operations (such as displaying the time in UTC).
|
274 |
|
275 | ``` typescript
|
276 | @Component({
|
277 | selector: 'app',
|
278 | template: `
|
279 | Last updated: {{ '2016-12-31T23:00:00.000-01:00' | amUtc | amDateFormat: 'YYYY-MM-DD' }}
|
280 | `
|
281 | })
|
282 | ```
|
283 |
|
284 | Prints `Last updated: 2017-01-01`
|
285 |
|
286 | Complete Example
|
287 | ----------------
|
288 |
|
289 | ``` typescript
|
290 | import { NgModule, Component } from '@angular/core';
|
291 | import { BrowserModule } from '@angular/platform-browser';
|
292 | import { platformBrowserDynamic } from '@angular/platform-browser-dynamic';
|
293 | import { MomentModule } from 'angular2-moment';
|
294 |
|
295 | @Component({
|
296 | selector: 'app',
|
297 | template: `
|
298 | Last updated: <b>{{myDate | amTimeAgo}}</b>, <b>{{myDate | amCalendar}}</b>, <b>{{myDate | amDateFormat:'LL'}}</b>
|
299 | `
|
300 | })
|
301 | export class AppComponent {
|
302 | myDate: Date;
|
303 |
|
304 | constructor() {
|
305 | this.myDate = new Date();
|
306 | }
|
307 | }
|
308 |
|
309 | @NgModule({
|
310 | imports: [
|
311 | BrowserModule,
|
312 | MomentModule
|
313 | ],
|
314 | declarations: [ AppComponent ]
|
315 | bootstrap: [ AppComponent ]
|
316 | })
|
317 | class AppModule {}
|
318 |
|
319 | platformBrowserDynamic().bootstrapModule(AppModule);
|
320 | ```
|
321 |
|
322 | Demo
|
323 | ----
|
324 |
|
325 | [See online demo on Plunker](http://plnkr.co/edit/ziBJ0mftSjnz0SrYPwbo?p=preview)
|