1

I have implemented p-calendar from primeNg and When I trying to compare/check dates it will return the wrong month.

<p-calendar
    [locale]="nl"
    [inline]="true"
    [showOtherMonths]="false"
    [showWeek]="true"
    [styleClass]="'ic-calendar'">
    <ng-template pTemplate="date" let-date>
      <span [class.maintenance]="checkDate(date)">{{date.day}}</span>
    </ng-template>
  </p-calendar>

If I change the {{date.day}} to {{date.month}} (or add it to just display the month) it results in 5 instead of 6 for the month June (current month at the time being).

in my typescript I configured the p-calendar:

this.nl = {
      firstDayOfWeek: 1,
      dayNames: ['Zondag', 'Maandag', 'Dinsdag', 'Woensdag', 'Donderdag', 'Vrijdag', 'Zaterdag'],
      dayNamesShort: ['Zon', 'Maa', 'Din', 'Woe', 'Don', 'Vrij', 'Zat'],
      dayNamesMin: ['Zo', 'Ma', 'Di', 'Wo', 'Do', 'Vr', 'Za'],
      // tslint:disable-next-line: max-line-length
      monthNames: [ 'Januari', 'Februari', 'Maart', 'April', 'Mei', 'Juni', 'Juli', 'Augustus', 'September', 'Oktober', 'November', 'December' ],
      monthNamesShort: [ 'Jan', 'Feb', 'Mar', 'Apr', 'Mei', 'Jun', 'Jul', 'Aug', 'Sep', 'Okt', 'Nov', 'Dec' ],
      dateFormat: 'dd/mm/yyyy',
      weekHeader: 'Wk'
    };
Babulaas
  • 761
  • 3
  • 13
  • 47
  • 1
    See https://stackoverflow.com/questions/2552483/why-does-the-month-argument-range-from-0-to-11-in-javascripts-date-constructor – Antikhippe Jun 30 '20 at 09:29

1 Answers1

0

In javascript month is the only one that used a zero based index.

The date object that is used in the p-calendar component is a custom object and not a Date object.

https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Date

you need the create a new Date from this object.

for example:

checkDate(date: any) {
    const d = new Date(date.year, date.month, date.day);

    // do something with the date
  }
}
Babulaas
  • 761
  • 3
  • 13
  • 47