How to Create Moment Pipe in Angular?

MomentPipe.ts

import { Pipe, PipeTransform } from '@angular/core';
import * as moment from 'moment';
@Pipe({ name: 'dateFormat' })
export class MomentPipe implements PipeTransform {
transform(value: Date | moment.Moment, dateFormat: string): any {
return moment(value).format(dateFormat);
}
}

You must include your pipe in the declarations array of the AppModule.

import { NgModule } from '@angular/core';
import { AppComponent } from './app.component';
import { MomentPipe } from './momentPipe';
@NgModule({
imports: [
// Your Modules
],
declarations: [
AppComponent,
// Your Components
MomentPipe
],
providers: [
// Your Providers
]
})
export class AppModule { }

Use it in your View/Html like below.,

{{ createdDate | dateFormat: 'MMMM Do YYYY, h:mm:ss a'}}

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store