Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to get custom date format value from NGX-Bootstrap datepicker

I m using NGX-Bootstrap for my angular6 project. I have reactive form and added datepicker to it and while returning the value from datepicker getting full format ex: "2018-11-07T05:12:35.000Z". But I want in MM-DD-YYYY.

like image 317
duddu venkatesh Avatar asked Nov 23 '18 05:11

duddu venkatesh


People also ask

How do I change the Datepicker format in bootstrap?

Go to line 1399 and find format: 'mm/dd/yyyy' . Now you can change the date format here.

What is Bsconfig?

bsconfig. json is the single, mandatory build meta file needed for rescript . The complete configuration schema is here. We'll non-exhaustively highlight the important parts in prose below.

What is the use of bootstrap Datepicker in angular?

Angular Bootstrap 5 Datepicker. Date picker is a plugin that adds the function of selecting time without the necessity of using custom JavaScript code.


2 Answers

Before returning the date, convert the date format as the following:

let date = new Date(dateValue).toLocaleDateString();

then assign date to the form controller

like image 66
Cyber Progs Avatar answered Oct 18 '22 21:10

Cyber Progs


to be more generic you can create a directive which returns the date in the format you want. for eg.

<input changeDateFormat type="text" placeholder="Datepicker" class="form-control" #dp="bsDatepicker" bsDatepicker [(bsValue)]="bsValue">

export class changeDateFormatDirective implements OnChanges {
@Input() bsValue: Date;
@Output() bsValueChange = new EventEmitter();

constructor(private cdr: ChangeDetectorRef) { }

ngOnChanges(changes: SimpleChanges) {
//change Date format here, you can import format date from @angular/common
this.bsValueChange.emit(newDateFormat);
this.cdr.detectChanges();
}
}
like image 1
Rahul K Pandey Avatar answered Oct 18 '22 22:10

Rahul K Pandey