user2128702
user2128702

Reputation: 2121

How to achieve date filtering in Angular2

I have a class inside of which I want to use date filtering as it used to be in Angular 1:

$filter('date')(startDate, 'yyyy-MM-dd HH:mm:ss')

Now I want to achieve this using Angular 2. As far as I can see, I could use DatePipe class. But the problem is that I don't know how to import it inside of my class file:

import { Injectable } from '@angular/core'
import { Http, Response, Headers } from '@angular/http';
import { InputValidatorService }  from './input-validator.service';
import { Pipe, PipeTransform } from '@angular/core';

...

myFunctionInsideOfAClass(){
   var datePipe = new DatePipe(); // Symbol 'DatePipe' can not be properly resolved, probably it is located in inaccessible module.
}

How can I achieve this?

EDIT: My app module:

import { NgModule }      from '@angular/core';
import { BrowserModule } from '@angular/platform-browser';
import { FormsModule }   from '@angular/forms';

..
@NgModule({
    imports: [BrowserModule, FormsModule, HttpModule],
    declarations: [AppComponent, MapComponent, AtileLayerDirective, MapDirective, mapControlDirective, ToolsComponent, SearchComponent],
    bootstrap: [AppComponent],
    providers: [{
        provide: Http,
        useFactory: (_backend: ConnectionBackend, _defaultOptions: RequestOptions) => new HttpInterceptor(_backend, _defaultOptions),
        deps: [XHRBackend, RequestOptions]
    }, mapManager, SearchService, StatusIconProvider
    ]
})

Upvotes: 1

Views: 842

Answers (1)

Günter Zöchbauer
Günter Zöchbauer

Reputation: 657356

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

@NgModule({
  ...
  providers: [ /* other providers */, DatePipe],
  ...
})

Upvotes: 1

Related Questions