ng2-datepicker options not working

5.5k Views Asked by At

I am trying to integrate ng2-datepicker in my project.

It worked fine until I tried to use the options. According to the documentation, I can pass options to the component using property bindings. So I tried to customize the format :

<ng2-datepicker [(ngModel)]="date" viewFormat="DD-MM-YYYY" name="date"></ng2-datepicker>

But the date is still displayed in the wrong format (and which is not the one that is supposed to be the default format according to the documentation).

I also tried to open the calendar by default with the option opened and it also did not work.

I looked at the referenced issues on github and none mentions this problem which is yet obvious. That makes me think that I misunderstood something.

Any ideas?

3

There are 3 best solutions below

0
Arnaud Denoyelle On BEST ANSWER

The documentation was not up to date. Now, we can pass options using the "options" attribute.

In mycomponent.html :

<ng2-datepicker [(ngModel)]="date" name="date" [options]="calendarOptions"></ng2-datepicker>

In mycomponent.ts :

export class MyComponent implements OnInit {
  calendarOptions = {
    format: "DD-MM-YYYY",
    firstWeekdaySunday: false
  };
0
Mohit Jain On
this.datepickerOptions = new DatePickerOptions({
          format: 'DD-MM-YYYY'
 });

Use inside HTML

< ng2-datepicker name="dateExpires" [options]="datepickerOptions ">
0
Vibhu kumar On
<ng-datepicker [options]="options"> </ng-datepicker>