Answers for "how to use datepipe in ts file"

0

how to use datepipe in ts file

// Since CommonModule does not export it as a provider you'll have to do it yourself. This is not very complicated.

//1) Import DatePipe:

import { DatePipe } from '@angular/common';
// 2) Include DatePipe in your module's providers:

NgModule({
  providers: [DatePipe]
})
export class AppModule {
}
// or component's providers:

@Component({
  selector: 'home',
  styleUrls: ['./home.component.css'],
  templateUrl: './home.component.html',
  providers: [DatePipe]
})
export class HomeComponent {
...
// 3) Inject it into your component's constructor like any other service:

constructor(private datePipe: DatePipe) {
}
// 4) Use it:

ngOnInit() {
    this.time = this.datePipe.transform(new Date());
}
Posted by: Guest on August-24-2021

Code answers related to "how to use datepipe in ts file"

Code answers related to "Javascript"

Browse Popular Code Answers by Language