How to use angular2 built-in date feed in services and script file directives

I need to use the angular2 date protocol in services and script file directives (not only in HTML).

Does anyone have any ideas?

Unable to download code with some political restrictions, sorry for that.

+6
source share
2 answers

Since the CommonModule does not export it as a provider, you will have to do it yourself. It is not very difficult.

1) Import DatePipe:

import { DatePipe } from '@angular/common'; 

2) Include DatePipe in the module providers:

 NgModule({ providers: [DatePipe] }) export class AppModule { } 

or component suppliers:

 @Component({ selector: 'home', styleUrls: ['./home.component.css'], templateUrl: './home.component.html', providers: [DatePipe] }) export class HomeComponent { ... 

3) Add it to the component constructor, like any other service:

 constructor(private datePipe: DatePipe) { } 

4) Use it:

 ngOnInit() { this.time = this.datePipe.transform(new Date()); } 
+20
source

Try something like this:

 new DatePipe().transform(myDate, 'yyyy-dd-MM'); 

Hope this helps.

+4
source

Source: https://habr.com/ru/post/1013414/


All Articles