Search code examples
angularangular2-routingangular2-formsangular-filters

Angular 2 import DatePipe and transform not working


I am using Angular 2 to get exception free date, such a case I get '0000-00-00' date from server and throw exceptions so I followed Pipe for filter.

{{"2016-05-11" | dateFormat:task_.date_format}} 

Pipe Code -

import { Pipe, PipeTransform } from '@angular/core';
import { DatePipe } from '@angular/common';
import { TaskService } from '../providers/taskService';

@Pipe({name: 'dateFormat'})
export class DateFormatPipe implements PipeTransform{


    constructor(public taskService: TaskService,private datePipe: DatePipe){}

    transform(value :any[],arg:any): any  {

        console.log("dateFormat value "+value+"\n arg "+arg);

        let format_ = this.taskService.getDateFormate(arg);
        console.log("format_ ",format_)
        let formatted = this.datePipe.transform(value, format_);
        console.log("formatted "+formatted);
        // if (value == this.taskService.getDateFormate(arg)) {
        return value;
        // }
    }
}

But it is not working , i just want that if proper date then return with selected i.e. yyyy-mm-dd, mm-dd-yyyy so on, otherwise return 0000-00-00.


Solution

  • Give variable type like

    datePipeEn: DatePipe = new DatePipe('en-US');
    

    And don't forget 'en-Us' which language code then use transform method.