1

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?

3 Answers 3

4

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');
Sign up to request clarification or add additional context in comments.

2 Comments

why you having two dates in it "Wed May 01 2019 14:40:27 GMT+0300 (GMT+03:00),Sat Jun 15 2019 14:40:27 GMT+0300 (GMT+03:00)" only send one it will work @tolga
It's dateRangePicker, so it has two dates in it (From and To) and event emits these dates as an array.
1

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.

Comments

0

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.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.