{"id":21,"date":"2013-06-28T05:14:00","date_gmt":"2013-06-27T23:44:00","guid":{"rendered":"https:\/\/www.inogic.com\/blog\/?p=21"},"modified":"2021-08-25T11:24:59","modified_gmt":"2021-08-25T11:24:59","slug":"how-to-convert-the-datetime-in-local-and-utc-date-time-format","status":"publish","type":"post","link":"https:\/\/www.inogic.com\/blog\/2013\/06\/how-to-convert-the-datetime-in-local-and-utc-date-time-format\/","title":{"rendered":"How to convert the DateTime in Local and UTC Date Time format"},"content":{"rendered":"<p>While working with the plug-ins or with Custom Workflows Assemblies, we get the Datetime from CRM which doesn\u2019t match with user\u2019s Local DateTime format.<\/p>\n<div><\/div>\n<div>As we get the date from the CRM in UTC Format so we can convert it to User\u2019s Local DateTime using <b>LocalTimeFromUtcTimeRequest <\/b>Request.<\/div>\n<div><\/div>\n<div>To get the Current logged in user\u2019s Local time You need to first retrieve the Time Zone code of that user using RetrieveCurrentUsersSettings request and then you need to convert the UTC date into user\u2019s Local DateTime.<\/div>\n<div><\/div>\n<div>We can also convert the Date from Local Datetime format \u00a0to the UTC Datetime Format.<\/div>\n<div><\/div>\n<div>Below examples illustrates how to write <b>LocalTimeFromUtcTimeRequest<\/b> \u00a0and <b>UtcTimeFromLocalTimeRequest<\/b> request to convert the Date.<\/div>\n<div><\/div>\n<div><b>Example : <\/b><\/div>\n<div><\/div>\n<div>First retrieve the time zone code from UserSettings entity and then excecute request <b>LocalTimeFromUtcTimeRequest<\/b> to convert the DateTime from UTC Format to Local DateTime format and <b>UtcTimeFromLocalTimeRequest<\/b> to convert the DateTime from Local to UTC DateTime format as given below.<\/div>\n<div><\/div>\n<div>\nDateTime convertDate = new DateTime();<\/div>\n<p>&nbsp;<\/p>\n<div><\/div>\n<p>&nbsp;<\/p>\n<div>\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\/\/get the Time Zone Code of user<\/div>\n<p>&nbsp;<\/p>\n<div>\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0 int? getTimeZoneCode = RetrieveCurrentUsersSettings(service);<\/div>\n<p>&nbsp;<\/p>\n<div><\/div>\n<p>&nbsp;<\/p>\n<div>\/\/Convert the UTC Date time into Users Local DateTime Format using request LocalTimeFromUtcTimeRequest<\/div>\n<p>&nbsp;<\/p>\n<div>\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0 DateTime localDateTime = RetrieveLocalTimeFromUTCTime(convertDate, getTimeZoneCode, service);<\/div>\n<p>&nbsp;<\/p>\n<div>\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0 \/\/Convert the Local Date time into UTC DateTime Format using request UtcTimeFromLocalTimeRequest<\/div>\n<p>&nbsp;<\/p>\n<div>\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0 DateTime utcDateTime = RetrieveUTCTimeFromLocalTime(convertDate, getTimeZoneCode, service);<\/div>\n<p>&nbsp;<\/p>\n<div><\/div>\n<p>&nbsp;<\/p>\n<div>\n<p>\u00a0 \u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\/\/\/<\/p>\n<\/div>\n<p>&nbsp;<\/p>\n<div>\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0 \/\/\/ Retrieves the current users timezone code<\/div>\n<p>&nbsp;<\/p>\n<div>\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0 \u00a0\/\/\/&nbsp;<\/p>\n<\/div>\n<p>&nbsp;<\/p>\n<div>\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0 \/\/\/  IOrganizationService<\/div>\n<p>&nbsp;<\/p>\n<div>\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\/\/\/<\/div>\n<p>&nbsp;<\/p>\n<div>\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0 private int? RetrieveCurrentUsersSettings(IOrganizationService service)<\/div>\n<p>&nbsp;<\/p>\n<div>\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0 {<\/div>\n<p>&nbsp;<\/p>\n<div>\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0 var currentUserSettings = service.RetrieveMultiple(<\/div>\n<p>&nbsp;<\/p>\n<div>\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0 new QueryExpression(&#8220;usersettings&#8221;)<\/div>\n<p>&nbsp;<\/p>\n<div>\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0 {<\/div>\n<p>&nbsp;<\/p>\n<div>\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0 ColumnSet = new ColumnSet(&#8220;timezonecode&#8221;),<\/div>\n<p>&nbsp;<\/p>\n<div>\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0 Criteria = new FilterExpression<\/div>\n<p>&nbsp;<\/p>\n<div>\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0 {<\/div>\n<p>&nbsp;<\/p>\n<div>\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0 Conditions =<\/div>\n<p>&nbsp;<\/p>\n<div>\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0 {<\/div>\n<p>&nbsp;<\/p>\n<div>\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0 \u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0new ConditionExpression(&#8220;systemuserid&#8221;, ConditionOperator.EqualUserId)<\/div>\n<p>&nbsp;<\/p>\n<div>\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0 }<\/div>\n<p>&nbsp;<\/p>\n<div>\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0 }<\/div>\n<p>&nbsp;<\/p>\n<div>\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0 }).Entities[0].ToEntity&lt;Entity&gt;();<\/div>\n<p>&nbsp;<\/p>\n<div>\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0 \/\/return time zone code<\/div>\n<p>&nbsp;<\/p>\n<div>\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0 return (int?)currentUserSettings.Attributes[&#8220;timezonecode&#8221;];<\/div>\n<p>&nbsp;<\/p>\n<div>\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0 }<\/div>\n<p>&nbsp;<\/p>\n<div><\/div>\n<p>&nbsp;<\/p>\n<div>\n<p>\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0 \/\/\/<\/p>\n<\/div>\n<p>&nbsp;<\/p>\n<div>\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0 \/\/\/\u00a0 Retrive the local time from the UTC time.<\/div>\n<p>&nbsp;<\/p>\n<div>\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0 \/\/\/&nbsp;<\/p>\n<\/div>\n<p>&nbsp;<\/p>\n<div>\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0 \/\/\/ UTC Date time which needs to convert to Local DateTime<\/div>\n<p>&nbsp;<\/p>\n<div>\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0 \/\/\/ TimeZoneCode<\/div>\n<p>&nbsp;<\/p>\n<div>\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0 \/\/\/ IOrganizationService service<\/div>\n<p>&nbsp;<\/p>\n<div>\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0 \/\/\/<\/div>\n<p>&nbsp;<\/p>\n<div>\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0 private DateTime RetrieveLocalTimeFromUTCTime(DateTime utcTime, int? timeZoneCode, IOrganizationService service)<\/div>\n<p>&nbsp;<\/p>\n<div>\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0 {<\/div>\n<p>&nbsp;<\/p>\n<div>\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0 if (!timeZoneCode.HasValue)<\/div>\n<p>&nbsp;<\/p>\n<div>\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0 return DateTime.Now;<\/div>\n<p>&nbsp;<\/p>\n<div>\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0 var request = new LocalTimeFromUtcTimeRequest<\/div>\n<p>&nbsp;<\/p>\n<div>\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0 {<\/div>\n<p>&nbsp;<\/p>\n<div>\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0 TimeZoneCode = timeZoneCode.Value,<\/div>\n<p>&nbsp;<\/p>\n<div>\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0 UtcTime = utcTime.ToUniversalTime()<\/div>\n<p>&nbsp;<\/p>\n<div>\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0 };<\/div>\n<p>&nbsp;<\/p>\n<div>\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0 var response = (LocalTimeFromUtcTimeResponse)service.Execute(request);<\/div>\n<p>&nbsp;<\/p>\n<div>\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0 return response.LocalTime;<\/div>\n<p>&nbsp;<\/p>\n<div>\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0 }<\/div>\n<p>&nbsp;<\/p>\n<div><\/div>\n<p>&nbsp;<\/p>\n<div>\n<p>\u00a0\u00a0\u00a0 \u00a0\u00a0\u00a0\u00a0\/\/\/<\/p>\n<\/div>\n<p>&nbsp;<\/p>\n<div>\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0 \/\/\/\u00a0 Retrive the UTC DateTime from Local Date time format.<\/div>\n<p>&nbsp;<\/p>\n<div>\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0 \/\/\/&nbsp;<\/p>\n<\/div>\n<p>&nbsp;<\/p>\n<div>\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0 \/\/\/ Local Date time which needs to convert to UTC<\/div>\n<p>&nbsp;<\/p>\n<div>\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0 \/\/\/ TimeZoneCode<\/div>\n<p>&nbsp;<\/p>\n<div>\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0 \/\/\/ IOrganizationService service<\/div>\n<p>&nbsp;<\/p>\n<div>\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0 \/\/\/<\/div>\n<p>&nbsp;<\/p>\n<div>\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0 private DateTime RetrieveUTCTimeFromLocalTime(DateTime localTime, int? timeZoneCode, IOrganizationService service)<\/div>\n<p>&nbsp;<\/p>\n<div>\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0 {<\/div>\n<p>&nbsp;<\/p>\n<div>\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0 if (!timeZoneCode.HasValue)<\/div>\n<p>&nbsp;<\/p>\n<div>\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0 return DateTime.Now;<\/div>\n<p>&nbsp;<\/p>\n<div><\/div>\n<p>&nbsp;<\/p>\n<div>\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0 var request = new UtcTimeFromLocalTimeRequest<\/div>\n<p>&nbsp;<\/p>\n<div>\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0 {<\/div>\n<p>&nbsp;<\/p>\n<div>\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0 TimeZoneCode = timeZoneCode.Value,<\/div>\n<p>&nbsp;<\/p>\n<div>\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0 LocalTime = localTime<\/div>\n<p>&nbsp;<\/p>\n<div>\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0 };<\/div>\n<p>&nbsp;<\/p>\n<div>\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0 var response = (UtcTimeFromLocalTimeResponse)service.Execute(request);<\/div>\n<p>&nbsp;<\/p>\n<div>\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0 return response.UtcTime;<\/div>\n<p>&nbsp;<\/p>\n<div>\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0 }<\/div>\n<h2 style=\"text-align: left;\"><div class=\"su-heading su-heading-style-default su-heading-align-center\" id=\"\" style=\"font-size:15px;margin-bottom:5px\"><div class=\"su-heading-inner\">Free 70% of storage space in CRM with Attachment Management Apps!<\/div><\/div><\/h2>\n<p><em><strong><a href=\"https:\/\/www.inogic.com\/product\/productivity-apps\/attach-2-dynamics-365-crm-upload-multiple-files-sharepoint-cloud-storage\" target=\"_blank\" rel=\"noopener noreferrer\">Attach2Dynamics<\/a> &#8211; Store and manage documents\/attachments in cloud storage of your choice &#8211; SharePoint, Dropbox or Azure Blob Storage from within Dynamics 365 CRM.<\/em><br \/>\n<em><strong><a href=\"https:\/\/www.inogic.com\/product\/productivity-apps\/dynamics-365-crm-sharepoint-security-metadata-sync\" target=\"_blank\" rel=\"noopener noreferrer\">SharePoint Security Sync<\/a><\/strong> \u2013 Robust and secure solution to integrate Dynamics 365 CRM and SharePoint Security Sync thereby ensuring secure access to confidential documents stored in SharePoint.<\/em><\/p>\n","protected":false},"excerpt":{"rendered":"<p>While working with the plug-ins or with Custom Workflows Assemblies, we get the Datetime from CRM which doesn\u2019t match with user\u2019s Local DateTime format. As we get the date from the CRM in UTC Format so we can convert it to User\u2019s Local DateTime using LocalTimeFromUtcTimeRequest Request. To get the Current logged in user\u2019s Local\u2026 <span class=\"read-more\"><a href=\"https:\/\/www.inogic.com\/blog\/2013\/06\/how-to-convert-the-datetime-in-local-and-utc-date-time-format\/\">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":[19,20,21,22,24],"tags":[459,1003,1713,1785],"class_list":["post-21","post","type-post","status-publish","format-standard","hentry","category-dynamics-crm","category-dynamics-crm-2011","category-dynamics-crm-2013","category-dynamics-crm-2015","category-dynamics-crm-2016","tag-custom-workflows-assemblies","tag-local-time-zone-in-crm-2011","tag-time-zone","tag-utc-time-format-in-crm"],"aioseo_notices":[],"_links":{"self":[{"href":"https:\/\/www.inogic.com\/blog\/wp-json\/wp\/v2\/posts\/21","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=21"}],"version-history":[{"count":0,"href":"https:\/\/www.inogic.com\/blog\/wp-json\/wp\/v2\/posts\/21\/revisions"}],"wp:attachment":[{"href":"https:\/\/www.inogic.com\/blog\/wp-json\/wp\/v2\/media?parent=21"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.inogic.com\/blog\/wp-json\/wp\/v2\/categories?post=21"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.inogic.com\/blog\/wp-json\/wp\/v2\/tags?post=21"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}