Search code examples
javascriptangularbootstrap-4ngx-bootstrap

Date format in Bootstrap Date Picker in Angular 6


In Angular 6, I apply date picker with a bsDaterangepicker class for a date range.

<input type="text" (ngModelChange)="dateFilterChanged($event)" [(ngModel)]="myDateField" value="{{ myDateField | date:'yyyy-MM-dd' }}" [bsConfig]="{dateInputFormat: 'yyyy-MM-dd'}" bsDaterangepicker>

and emit the value with the following function and emitter:

dateFilterChanged(filterValue: string) {
    console.log('filterValue', filterValue);
    this.dateFilterChanged.emit(filterValue)
}

The problem is, the format of the emitted date is not "yyyy-MM-dd", but a gmt string:

[Wed May 01 2019 14:04:12 GMT+0300 (GMT+03:00), Sat Jun 15 2019 14:04:12 GMT+0300 (GMT+03:00)]

How can I get emit the date value in "yyyy-MM-dd" format?


Solution

  • You can use date pipe like this :

    {{ value_expression | date [ : format [ : timezone [ : locale ] ] ] }}
    

    For more information visit this link :

    https://angular.io/api/common/DatePipe

    you can use it in typescript also ::

    import { DatePipe } from '@angular/common';
    
    constructor(private datePipe: DatePipe,){
    }
    // in your function 
     element.last_assessment_date = this.datePipe.transform(element.last_assessment_date, 'yyyy-MM-dd');