2

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.

Mistalis
  • 17,793
  • 13
  • 73
  • 97
Shivam Sharma
  • 23
  • 1
  • 5

2 Answers2

1

Give variable type like

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

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

Code_S
  • 194
  • 2
  • 12
0

Have you included your pipe in the bootstrap of your app.module file (declarations array)?

Check then the typo in your statement (guess Formate should be Format) let format_ = this.taskService.getDateFormate(arg);

Except this, are you aware that Angular2 has it's own implementation of the Date pipe? Additionally, as stated in the docs, if you're using Safari or older browsers you have to include the ECMAScript Internationalization API

Please check your variable naming convention (task_ doesn't look so good in my opinion...)

Andrea Alhena
  • 986
  • 6
  • 9
  • Actually, I have checked already. I Just want to get exception free , which is getting when I get "0000-00-00" from server and that time 'date:yyyy-mm-dd' or more throw exception "Invalid Time". – Shivam Sharma Dec 14 '16 at 13:01
  • I was following - http://stackoverflow.com/a/37859544/6547558 & http://stackoverflow.com/a/35144872/6547558 – Shivam Sharma Dec 14 '16 at 13:04
  • var datePipe:any = new DatePipe(); Throw Error - Supplied parameters do not match any signature of call target. why? – Shivam Sharma Dec 14 '16 at 13:33
  • You get an error because of your constructor signature: there are two injected service that you are not supplying. The Pipe should be declared in the declarations array in the app.module file, then imported and used. I don.t get the reason why you are trying to create a new DatePipe object – Andrea Alhena Dec 14 '16 at 14:09