简体   繁体   中英

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?

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');

You can use Date Pipe for this.
Example:

let dateObj = Your object;

And use the Pipe like below.

{{ dateObj | date }} // output is 'Jun 15, 2015'

Or there is a JS library called moment.

You can get that from https://momentjs.com/ and follow the instruction to install it. And they have mentioned all the Format Dates. Just pass the GMT string to moment() and you will get your desired format.

You can get that from https://momentjs.com/ and follow the instruction to install it. And they have mentioned all the Format Dates. Just pass the GMT string to moment() and you will get your desired format.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM