showing results for - "how to use datepipe in ts file"
Branden
19 Apr 2020
1// Since CommonModule does not export it as a provider you'll have to do it yourself. This is not very complicated.
2
3//1) Import DatePipe:
4
5import { DatePipe } from '@angular/common';
6// 2) Include DatePipe in your module's providers:
7
8NgModule({
9  providers: [DatePipe]
10})
11export class AppModule {
12}
13// or component's providers:
14
15@Component({
16  selector: 'home',
17  styleUrls: ['./home.component.css'],
18  templateUrl: './home.component.html',
19  providers: [DatePipe]
20})
21export class HomeComponent {
22...
23// 3) Inject it into your component's constructor like any other service:
24
25constructor(private datePipe: DatePipe) {
26}
27// 4) Use it:
28
29ngOnInit() {
30    this.time = this.datePipe.transform(new Date());
31}
32