Dynamics 365 WEB API · Dynamics CRM Javascript · Javascript

Dynamics CRM 365 Get current Date Time based on logged in User Timezone through Javascript

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 –

  1. 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
  2. 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
  3. 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!

2 thoughts on “Dynamics CRM 365 Get current Date Time based on logged in User Timezone through Javascript

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s