{"id":26141,"date":"2020-12-24T12:54:31","date_gmt":"2020-12-24T12:54:31","guid":{"rendered":"https:\/\/www.inogic.com\/blog\/?p=26141"},"modified":"2020-12-28T10:38:16","modified_gmt":"2020-12-28T10:38:16","slug":"convert-time-into-user-time-using-time-zone-codes","status":"publish","type":"post","link":"https:\/\/www.inogic.com\/blog\/2020\/12\/convert-time-into-user-time-using-time-zone-codes\/","title":{"rendered":"Convert Time into User Time Using Time Zone Codes"},"content":{"rendered":"<h1>Introduction<\/h1>\n<p style=\"text-align: justify;\">Recently we had one requirement in which we need to set the date field by user time zone. In this requirement using javascript, the manager creates (with the help of HTML web resource) multiple Appointment entity records dynamically for different users as owner and set date field (Start Time and End Time) as per owner time zone.<br \/>\nFirstly, we retrieve the time zone code information of the user. As per the fetchXml below, we read the time zone code information of the user by passing GUID of the user.<\/p>\n<p>\/\/Read the time zone codes of user<br \/>\nfunction setUserDateTimeZone(userId) {<\/p>\n<p>\/\/generate the fetchXml<br \/>\nvar fetchXml = &#8220;&lt;fetch version=&#8217;1.0&#8242; output-format=&#8217;xml-platform&#8217; mapping=&#8217;logical&#8217; distinct=&#8217;false&#8217;&gt;&#8221;;<br \/>\nfetchXml += &#8220;&lt;entity name=&#8217;usersettings&#8217;&gt;&#8221;;<br \/>\nfetchXml += &#8220;&lt;attribute name=&#8217;timezonecode&#8217; \/&gt;&#8221;;<br \/>\nfetchXml += &#8220;&lt;attribute name=&#8217;timezonedaylightday&#8217; \/&gt;&#8221;;<br \/>\nfetchXml += &#8220;&lt;attribute name=&#8217;timezonedaylightminute&#8217; \/&gt;&#8221;;<br \/>\nfetchXml += &#8220;&lt;attribute name=&#8217;timezonedaylightyear&#8217; \/&gt;&#8221;;<br \/>\nfetchXml += &#8220;&lt;attribute name=&#8217;timezonedaylightsecond&#8217; \/&gt;&#8221;;<br \/>\nfetchXml += &#8220;&lt;attribute name=&#8217;timezonedaylightdayofweek&#8217; \/&gt;&#8221;;<br \/>\nfetchXml += &#8220;&lt;attribute name=&#8217;timezonedaylighthour&#8217; \/&gt;&#8221;;<br \/>\nfetchXml += &#8220;&lt;attribute name=&#8217;timezonedaylightmonth&#8217; \/&gt;&#8221;;<br \/>\nfetchXml += &#8220;&lt;attribute name=&#8217;timezonebias&#8217; \/&gt;&#8221;;<br \/>\nfetchXml += &#8220;&lt;attribute name=&#8217;timezonedaylightbias&#8217;\/&gt;&#8221;;<br \/>\nfetchXml += &#8220;&lt;attribute name=&#8217;businessunitid&#8217;\/&gt;&#8221;;<br \/>\nfetchXml += &#8220;&lt;filter&gt;&#8221;;<br \/>\nfetchXml += &#8220;&lt;condition attribute=&#8217;systemuserid&#8217; operator=&#8217;eq&#8217; value='&#8221;+userId+&#8221;&#8216;\/&gt;&#8221;;<br \/>\nfetchXml += &#8220;&lt;\/filter&gt;&#8221;;<br \/>\nfetchXml += &#8220;&lt;\/entity&gt;&lt;\/fetch&gt;&#8221;;<\/p>\n<p>\/\/get the result<br \/>\nfetchXml = &#8220;?fetchXml=&#8221; + encodeURIComponent(fetchXml);<\/p>\n<p>\/\/Fetch the user time zone information<br \/>\nXrm.WebApi.retrieveMultipleRecords(&#8216;usersettings&#8217;, fetchXml).then(function success(results) {<\/p>\n<p>\/\/create time Zone object<br \/>\nvar timeZoneObj = {};<br \/>\ntimeZoneObj.timezonecode = results.entities[0][&#8220;timezonecode&#8221;];<br \/>\ntimeZoneObj.timezonebias = results.entities[0][&#8220;timezonebias&#8221;];<br \/>\ntimeZoneObj.timezonedaylightbias = results.entities[0][&#8220;timezonedaylightbias&#8221;];<br \/>\ntimeZoneObj.timezonedaylightday = results.entities[0][&#8220;timezonedaylightday&#8221;];<br \/>\ntimeZoneObj.timezonedaylightminute = results.entities[0][&#8220;timezonedaylightminute&#8221;];<br \/>\ntimeZoneObj.timezonedaylightyear = results.entities[0][&#8220;timezonedaylightyear&#8221;];<br \/>\ntimeZoneObj.timezonedaylightsecond = results.entities[0][&#8220;timezonedaylightsecond&#8221;];<br \/>\ntimeZoneObj.timezonedaylightdayofweek = results.entities[0][&#8220;timezonedaylightdayofweek&#8221;];<br \/>\ntimeZoneObj.timezonedaylighthour = results.entities[0][&#8220;timezonedaylighthour&#8221;];<br \/>\ntimeZoneObj.timezonedaylightmonth = results.entities[0][&#8220;timezonedaylightmonth&#8221;];<br \/>\n},<br \/>\nfunction (error) {<br \/>\nconsole.log(error.message);<br \/>\n});<br \/>\n}<\/p>\n<p style=\"text-align: justify;\">After getting the time zone Codes of the user, we convert the passed date time into the usertime. For this, we pass the date string with default (&#8220;+00:00&#8221;) offset value and time zone codes of the user to generateUserTime function below:<\/p>\n<p>var dateValue = &#8220;2020-12-25 12:30:30+00:00&#8221;;<\/p>\n<p>\/\/convert the given time to user Time Zone time<br \/>\nvar generateUserTime = function (dateValue, timeZoneObj) {<\/p>\n<p>\/\/get the user time zone offset value<br \/>\nvar timeZoneOffset = findTimeZoneOffset(timeZoneObj);<br \/>\nif (isValid(timeZoneOffset)) {<br \/>\ndateValue = dateValue.split(&#8220;+00:00&#8221;);<br \/>\nreturn new Date(dateValue[0] + timeZoneOffset);<br \/>\n}<br \/>\n}<\/p>\n<p>\/\/function call to get current daylight offset<br \/>\nfunction findTimeZoneOffset(timeZone) {<br \/>\nvar actualTimeOffset = &#8220;&#8221;;<\/p>\n<p>\/\/get time zone offset value<br \/>\nvar timeOffset = timeZone.timezonebias;<\/p>\n<p>\/\/check if daylight is present or not<br \/>\nif (timeZone.timezonedaylightday != 0 || timeZone.timezonedaylightminute != 0 || timeZone.timezonedaylightyear != 0 || timeZone.timezonedaylightsecond != 0 || timeZone.timezonedaylightdayofweek != 0 || timeZone.timezonedaylighthour != 0 || timeZone.timezonedaylightmonth != 0) {<\/p>\n<p>\/\/get time zone daylight value<br \/>\ntimeOffset = (timeOffset + (timeZone.timezonedaylightbias));<br \/>\n}<\/p>\n<p>\/\/get actual time offset according time zone<br \/>\nif (isPostiveNegative(timeOffset) == -1) {<br \/>\ntimeOffset = parseInt(timeOffset.toString().split(&#8216;-&#8216;)[1]);<br \/>\nvar hours = Math.floor(timeOffset \/ 60);<br \/>\nvar minutes = timeOffset % 60;<br \/>\nactualTimeOffset = &#8220;+&#8221; + (hours.toString().length == 1 ? &#8220;0&#8221; + hours : hours) + &#8220;:&#8221; + (minutes.toString().length == 1 ? &#8220;0&#8221; + minutes : minutes);<br \/>\n} else if (isPostiveNegative(timeOffset) == 1) {<br \/>\nvar hours = Math.floor(timeOffset \/ 60);<br \/>\nvar minutes = timeOffset % 60;<br \/>\nactualTimeOffset = &#8220;-&#8221; + (hours.toString().length == 1 ? &#8220;0&#8221; + hours : hours) + &#8220;:&#8221; + (minutes.toString().length == 1 ? &#8220;0&#8221; + minutes : minutes);<br \/>\n} else if (isPostiveNegative(timeOffset) == 0) {<br \/>\nvar hours = Math.floor(timeOffset \/ 60);<br \/>\nvar minutes = timeOffset % 60;<br \/>\nactualTimeOffset = &#8220;+&#8221; + (hours.toString().length == 1 ? &#8220;0&#8221; + hours : hours) + &#8220;:&#8221; + (minutes.toString().length == 1 ? &#8220;0&#8221; + minutes : minutes);<br \/>\n}<br \/>\nreturn actualTimeOffset;<br \/>\n}<\/p>\n<p>\/\/Check number is postive or not<br \/>\nfunction isPostiveNegative(num) {<br \/>\nreturn num === 0 ? num : (num &gt; 0 ? 1 : -1);<br \/>\n}<\/p>\n<h2>Conclusion<\/h2>\n<p style=\"text-align: justify;\">Using user Time Zone codes, we can easily find and convert time into user time.<\/p>\n<p><a href=\"https:\/\/www.inogic.com\/product\/productivity-apps\/user-adoption-monitor-in-dynamics-crm\" target=\"_blank\" rel=\"noopener noreferrer\"><img decoding=\"async\" class=\"aligncenter wp-image-26142 size-full\" src=\"https:\/\/www.inogic.com\/blog\/wp-content\/uploads\/2020\/12\/user-adoption-monitor.jpg\" alt=\"\" width=\"800\" height=\"200\" srcset=\"https:\/\/www.inogic.com\/blog\/wp-content\/uploads\/2020\/12\/user-adoption-monitor.jpg 800w, https:\/\/www.inogic.com\/blog\/wp-content\/uploads\/2020\/12\/user-adoption-monitor-300x75.jpg 300w, https:\/\/www.inogic.com\/blog\/wp-content\/uploads\/2020\/12\/user-adoption-monitor-768x192.jpg 768w, https:\/\/www.inogic.com\/blog\/wp-content\/uploads\/2020\/12\/user-adoption-monitor-660x165.jpg 660w\" sizes=\"(max-width: 800px) 100vw, 800px\" \/><\/a><\/p>\n","protected":false},"excerpt":{"rendered":"<p>Introduction Recently we had one requirement in which we need to set the date field by user time zone. In this requirement using javascript, the manager creates (with the help of HTML web resource) multiple Appointment entity records dynamically for different users as owner and set date field (Start Time and End Time) as per\u2026 <span class=\"read-more\"><a href=\"https:\/\/www.inogic.com\/blog\/2020\/12\/convert-time-into-user-time-using-time-zone-codes\/\">Read More &raquo;<\/a><\/span><\/p>\n","protected":false},"author":13,"featured_media":0,"comment_status":"closed","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"om_disable_all_campaigns":false,"_monsterinsights_skip_tracking":false,"_monsterinsights_sitenote_active":false,"_monsterinsights_sitenote_note":"","_monsterinsights_sitenote_category":0,"footnotes":""},"categories":[16,19],"tags":[2101,1713,2103,2102],"class_list":["post-26141","post","type-post","status-publish","format-standard","hentry","category-dynamics-365","category-dynamics-crm","tag-convert-time","tag-time-zone","tag-time-zone-codes","tag-user-time"],"aioseo_notices":[],"_links":{"self":[{"href":"https:\/\/www.inogic.com\/blog\/wp-json\/wp\/v2\/posts\/26141","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.inogic.com\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.inogic.com\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.inogic.com\/blog\/wp-json\/wp\/v2\/users\/13"}],"replies":[{"embeddable":true,"href":"https:\/\/www.inogic.com\/blog\/wp-json\/wp\/v2\/comments?post=26141"}],"version-history":[{"count":0,"href":"https:\/\/www.inogic.com\/blog\/wp-json\/wp\/v2\/posts\/26141\/revisions"}],"wp:attachment":[{"href":"https:\/\/www.inogic.com\/blog\/wp-json\/wp\/v2\/media?parent=26141"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.inogic.com\/blog\/wp-json\/wp\/v2\/categories?post=26141"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.inogic.com\/blog\/wp-json\/wp\/v2\/tags?post=26141"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}