Dynamics CRM new Date() from javascript will return DateTime based on user system time but not based on logged in user timezone selected in personal settings.
In order to get get current Date Time based on logged in User Timezone through Javascript, we can use below code which will return Date time based on logged in user timezone.
function getUTCtoUserTime(userGuid) {
var dateValue = null;
Xrm.WebApi.online.retrieveMultipleRecords("usersettings", "?$select=timezonebias&$filter=systemuserid eq "+userGuid+"").then(
function success(results) {
var now = new Date();
dateValue = new Date(now.getUTCFullYear(), now.getUTCMonth(), now.getUTCDate(), now.getUTCHours(), now.getUTCMinutes(), now.getUTCSeconds());
var actMinutes = dateValue.getMinutes();
if (userSettingsObj[0].TimeZoneBias != null) {
dateValue.setMinutes(actMinutes + (results.entities[0]["timezonebias@OData.Community.Display.V1.FormattedValue"] * -1));
}
return dateValue;
},
function(error) {
Xrm.Utility.alertDialog(error.message);
return null;
}
);
}
Key Points –
- Code is making use of usersettings entity which stores all personal settings in CRM. So please assign Read access to custom security roles and any other role which does not have this permission
timezonebias value will contains minutes unit based on User personal settings timezone. For example if timezone is selected EST then this attribute value will have 300 which is UTC-5
- I would urge you to go through attributes of usersettings entity from Rest Builder. It does contains many useful information which can be utilized for different use case
Happy coding!
Reblogged this on Microsoft Dynamics 365 CRM.
LikeLiked by 1 person