kekeh / angular-mydatepicker

Angular datepicker and date range picker :date:
https://kekeh.github.io/angular-mydatepicker/
MIT License
1 stars 11 forks source link

dateFormat not respected when options set in dateChanged callback #93

Closed huffSamuel closed 3 years ago

huffSamuel commented 3 years ago

Description

I'm attempting to have the datepicker display the date ordinal as a day of the month rather than the entire date, for instance "3rd of the month" or "16th of the month". In the dateChanged callback I calculate the ordinal, create a new dateFormat, and copy the current options and reassign with the new date format. Sometimes this date format is respected immediately, sometimes the format lags one selection behind the current date.

Steps to Reproduce

See my source code here. Snippets below:

home.page.ts

...
options: IAngularMyDpOptions;
onDateChanged(event: IMyDateModel) {
  const date = event.singleDate.day;
  const copy = JSON.parse(JSON.stringify(this.options));
  copy.dateFormat = this.getDateFormat(date); // Returns 'd' + the ordinal ('st', 'nd', 'rd', th')
  this.options = copy;
}
...

home.page.html

...
    <input angular-mydatepicker #dp="angular-mydatepicker" [(ngModel)]="model" (dateChanged)="onDateChanged($event)" [options]="options"/>
...
  1. Open the calendar
  2. Select a date
  3. Select a date that ends in a different ordinal (1st, 2nd, 3rd, 4th, etc)
  4. Observe the ordinal lags behind the date

I can more reliably set the ordinal by updating the options, then updating the bound model in a setTimeout, but that isn't always effective.

Expected Results

kekeh commented 3 years ago

I added this kind of feature to the component. Install newest version of the component from npm (0.11.5).

In the dateFormat option you can define ordinal dates (d##) for example

dateFormat: 'd## of mmm yyyy'
huffSamuel commented 3 years ago

Works perfectly. Thank you!