I would like to implement moment.js library inside an angular2 project i order to convert a UTC
time to a certain time zone Europe/london
and that using moment
and [moment timezone]
1
So far, I have installed moment.js
in my Angular2 project using the following command:
npm install moment --save
Here is my current code:
import { Component, Pipe, PipeTransform } from '@angular/core';
import * as moment from 'moment';
@Pipe({ name: 'moment' })
class MomentPipe{
transform(date, format) {
return moment(date).format(format);
}
}
The Html:
I received the time from the backend as an object
//time.bookingTime.iso == 2016-07-20T21:00:00.000Z
{{time.bookingTime.iso | moment}}
It didn't work for me and I think that I have wrong implementation
Update: June 2018
Official Documentation about PIPES
To learn about Moment in Typescript and Moment Date Formats.
Example given below based on the Question here! Using
PIPE
technique we can use moment in Angular View as well (Date Formatting).MomentPipe.ts
You must include your pipe in the declarations array of the AppModule.
Use it in your View/Html like below.,
Hope this helps.,
Try below
When you need to use it, you have to specify it in the @component:
and use it in the html in this way:
btw, this is my way to write the pipe: