angularangular-materialformatdatetime

Apply pipe or filter only for specific type of data -Angular 5


<span class="date_value">{{SelectedDateText }}</span>
......
....
  <input matInput [matDatepicker]="picker"  [(ngModel)]="SelectedDateText"  >
......

The data type of "SelectedDateText" is string and it is also having string values in some cases like 'Today', 'Yesterday'. but when date is selected from matDatepicker it shows data in (Tue Feb 27 2018 00:00:00 GMT+0500 (Pakistan Standard Time)). I Can not apply filter on SelectedDateText as it is also containing strings. How can I manage to apply filter only when value is in date datatype or something else like this


Solution

  • Something like (with courtesy of Angular2 use basic pipe in custom pipe )

    EDITED after comment

    import {Pipe, PipeTransform} from '@angular/core';
    import {DatePipe} from "@angular/common";
    
    @Pipe({name: 'myDatePipe'})
    export class MyDatePipe extends DatePipe
    {
            transform(value: any, format?: string, timezone?: string, locale?: string): string {
            if (typeof value === "string") {
                return value; // return raw string
            } else {
                 return super.transform(arguments); // use the inherited datePipe transform method
            }
        }
    }
    

    should do the trick