Announcements
Hello,
I need to read Date value of one DateTime field in my Dynamics CRM Form using JavaScript.
I am able to read the value but it is not exactly same what I can see in CRM.Date t
In CRM Form, selected value is: 11 Nov 2020 8:00am
JavaScript Code return value as 11 Nov 2020 different time, as per UTC.
How do i make JS read write value
Hi Ravi,
You can use getTimeZoneOffsetMinutes function from global context as below to get the correct date and time value.
var dateTimeFieldValue = Xrm.Page.getAttribute("FieldName").getValue()
var utcDate = new Date(dateTimeFieldValue.getUTCFullYear() , dateTimeFieldValue.getUTCMonth() , dateTimeFieldValue.getUTCDate(), dateTimeFieldValue.getUTCHours(), dateTimeFieldValue.getUTCMinutes(), dateTimeFieldValue.getUTCSeconds() ,dateTimeFieldValue.getUTCMilliseconds());
var correctFieldDateTimeValue = new Date(utcDate.setMinutes(utcDate.getMinutes() + Xrm.Utility.getGlobalContext().userSettings.getTimeZoneOffsetMinutes()));
Hope this helps!
This is a general JavaScript date-time issue when dealing with date objects and UTC and browser local time.
Depending on exactly what you are wanting to do the answer may be slightly different, and the methods may be different, but on a Date object .toLocaleDateString() or .toLocaleDateString("<country code>") if you want it in a specific country timezone etc (also a similar method exists exists for the time component).
André Arnaud de Cal... 291,359 Super User 2024 Season 2
Martin Dráb 230,370 Most Valuable Professional
nmaenpaa 101,156