What I learned today — 11 January 2018

Niel de Wet
1 min readJan 11, 2018

--

Yesterday I mentioned that I didn’t understand PrimeNG’s p-calendar and its treatment of time zones and why the selected date is off by one day.

The problem is that by default the calendar returns the selected date at midnight in the local time zone, but since we are only interested in the date and discard the time portion (effectively treating the returned value as midnight UTC), the date is off by one day because South Africa is in UTC+2, which means midnight in the local time zone is 22:00 in UTC, or the previous day.

The solution is to tell p-calendar convert date to UTC on selection using the utc="true” property. For example:

<p-calendar id=”id” formControlName=”name” utc=”true”></p-calendar>

You can see this in action in this plnkr: https://plnkr.co/edit/dICdEA?p=preview

--

--

Responses (3)