Introduction
This article is about the date time format and zone issue in Dynamics 365 Portal. Dynamics 365 CE store date time in UTC format in CRM DB and when you will display date field on Dynamics 365 Portals it will display same date which sometime leads to incorrect date & time. If you are facing same issue this article can help you to fix it.
Details
When we print date and time in Portal pages using Liquid it is printed in the UTC format only, for example while printing booking details on the portal page, I noticed that it was displaying date and time with one day difference from CRM UI. As you can see in following screen the date and time in CRM UI
But in the portal it was displaying date and time like following
To overcome this issue we can use JS library like Moment.js which is included in the portal. But how to use these library, when I tried to look if someone already using it solve this issue, I found a hack to add an offset to date and time field to display date, so for example if we will use following it may display you correct date and time.
{% assign start = workorderresult['booking.starttime'] | date_add_hours: 10 %}
But this will an issue for the region with daylight saving time and it can start displaying wrong date and time when time will change. I tried different option to make it work without the offset value to avoid date time issue with daylight saving region, but I was not able to find any easy way to do in the Liquid, and you we can assign variable from the liquid to JS but we can’t assign a JS variable back to liquid variable. So to overcome this issue I displayed date time field under html table like following.
<td id="starttime" style="font-weight:bold"> {{start}} </td>
and after that used Moment.js like following to solve the daylight issue
<script type="text/javascript"> $(document).ready(function() { //set local moment.locale('en-AU'); //format date from UTC to local $('#starttime')[0].innerHTML=moment.utc($('#starttime')[0].innerHTML).local().format('dddd, Do MMM YYYY h:mm A'); $('#endtime')[0].innerHTML=moment.utc($('#endtime')[0].innerHTML).local().format('dddd, Do MMM YYYY h:mm A'); }); </script>
After above changes, I was able to see date and time correctly
Hope it will help someone !!
Pingback: Datetime zone issue in Liquid – Dynamics 365 Portals - Microsoft Dynamics CRM Community