Hello everyone and welcome to my blog. In today’s blog, we will discuss about a simple yet useful feature to convert UTC time to local time in Dynamics 365/ Model driven web resources.
Before we get to the solution, let’s understand when you shall require to use this functionality.
This scenario usually arise when you perform client side integration from your Dynamics 365/ Model driven client code. In few scenarios, you get the value of the date time fields in the integration in UTC format.
For example – If you are querying SharePoint from your javascript web resource files in Dynamics or Model driven apps, you will find the datetime values from SharePoint are returned in UTC format.
This is the date and time in UTC format as of the time of writing this blog – 2022-10-17T13:12:27Z
Let’s see how we can convert it to local time using client side Xrm API model. Dynamics 365/ Model driven client API has an action – LocalTimeToUtcTime to accomplish just that. Below is the code to execute this function from client side API.
// JavaScript source code
var localtimeTimeFromUTCTimeRequest = {};
localtimeTimeFromUTCTimeRequest.TimeZoneCode = 190; //timezone code for IST timezone. You need to replace with the timezone you want to return results for
localtimeTimeFromUTCTimeRequest.UtcTime = '2022-10-17T13:12:27Z';
localtimeTimeFromUTCTimeRequest.getMetadata = function () {
return {
boundParameter: null,
parameterTypes: {
"TimeZoneCode": {
"typeName": "Edm.Int32",
"structuralProperty": 1
},
"UtcTime": {
"typeName": "Edm.DateTimeOffset",
"structuralProperty": 1
}
},
operationType: 1,
operationName: "LocalTimeFromUtcTime"
}
};
Xrm.WebApi.online.execute(localtimeTimeFromUTCTimeRequest).
then(
function successCallback(result) {
if (result.ok) {
var data = await result.json();
}
},
function errorCallback(error) {
console.log(error.message);
}
);
The code is pretty straightforward except for the TimeZoneCode value which you need to provide. You can get the timezonecode by querying the Timezone table of DataVerse/ Dynamics 365. However I am providing the list of timezones with their associated code at the bottom of this post for your easy reference.
Hope you liked the post. If this post has helped, you can buy me a coffee. Links on the right side panel.
Debajit Dutta
Business Solutions MVP
LIST OF TIMEZONES AND THEIR CODES
(GMT+10:00) Canberra, Melbourne, Sydney (Commonwealth Games 2006); 256
(GMT-04:00) Georgetown, La Paz, Manaus, San Juan; 55
(GMT) Greenwich Mean Time : Dublin, Edinburgh, Lisbon, London; 85
(GMT+06:00) Ekaterinburg; 180
(GMT+03:00) Baghdad; 158
(GMT+06:00) Dhaka; 196
(GMT-06:00) Guadalajara, Mexico City, Monterrey; 29
(GMT+04:00) Abu Dhabi, Muscat; 165
(GMT+02:00) Beirut; 131
(GMT) Coordinated Universal Time; 92
(GMT+10:00) Canberra, Melbourne, Sydney; 255
(GMT+10:00) Brisbane; 260
(GMT-01:00) Cape Verde Is.; 83
(GMT+01:00) Brussels, Copenhagen, Madrid, Paris; 105
(GMT+12:00) Auckland, Wellington; 290
(GMT+2:00) Nicosia; 115
(GMT+02:00) Athens, Bucharest; 130
(GMT+13:00) Nukualofa; 300
(GMT+10:00) Hobart; 265
(GMT+04:00) Port Louis; 172
(GMT+07:00) Novosibirsk; 201
(GMT-03:30) Newfoundland; 60
(GMT+03:30) Tehran; 160
(GMT+08:00) Beijing, Chongqing, Hong Kong, Urumqi; 210
(GMT+02:00) Amman; 129
(GMT-06:00) Central America; 33
(GMT+08:00) Krasnoyarsk; 207
(GMT-11:00) Coordinated Universal Time-11; 6
(GMT+01:00) Sarajevo, Skopje, Warsaw, Zagreb; 100
(GMT-05:00) Bogota, Lima, Quito; 45
(GMT+10:00) Yakutsk; 240
(GMT+04:00) Yerevan; 170
(GMT+09:00) Osaka, Sapporo, Tokyo; 235
(GMT+10:00) Guam, Port Moresby; 275
(GMT+12:00) Fiji; 285
(GMT+04:30) Kabul; 175
(GMT-05:00) Eastern Time (US & Canada); 35
(GMT+11:00) Vladivostok; 270
(GMT-01:00) Azores; 80
(GMT+02:00) Jerusalem; 135
(GMT+01:00) Belgrade, Bratislava, Budapest, Ljubljana, Prague; 95
(GMT+07:00) Bangkok, Hanoi, Jakarta; 205
(GMT-03:00) Buenos Aires; 69
(GMT+09:00) Irkutsk; 227
(GMT+12:00) Magadan; 281
(GMT-03:00) Cayenne, Fortaleza; 70
(GMT-07:00) Chihuahua, La Paz, Mazatlan; 12
(GMT+02:00) Cairo; 120
(GMT-08:00) Baja California; 5
(GMT+12:00) Petropavlovsk-Kamchatsky - Old; 295
(GMT+05:00) Tashkent; 185
(GMT+03:00) Nairobi; 155
(GMT-04:00) Atlantic Time (Canada); 50
(GMT-12:00) International Date Line West; 0
(GMT+03:00) Kaliningrad, Minsk; 159
(GMT-02:00) Coordinated Universal Time-02; 76
(GMT+02:00) Istanbul; 134
(GMT+08:00) Perth; 225
(GMT+13:00) Samoa; 1
(GMT) Casablanca; 84
(GMT+06:00) Astana; 195
(GMT+09:30) Darwin; 245
(GMT-04:30) Caracas; 47
(GMT-10:00) Hawaii; 2
(GMT+09:00) Seoul; 230
(GMT+08:00) Kuala Lumpur, Singapore; 215
(GMT-06:00) Guadalajara, Mexico City, Monterrey - Old; 30
(GMT+06:30) Yangon (Rangoon); 203
(GMT+04:00) Moscow, St. Petersburg, Volgograd; 145
(GMT+02:00) Damascus; 133
(GMT-07:00) Arizona; 15
(GMT+04:00) Tbilisi; 173
(GMT-09:00) Alaska; 3
(GMT-03:00) Brasilia; 65
(GMT+01:00) Amsterdam, Berlin, Bern, Rome, Stockholm, Vienna; 110
(GMT-07:00) Chihuahua, La Paz, Mazatlan - Old; 13
(GMT+08:00) Ulaanbaatar; 228
(GMT-03:00) Greenland; 73
(GMT+09:30) Adelaide; 250
(GMT-02:00) Mid-Atlantic; 75
(GMT+12:00) Coordinated Universal Time+12; 284
(GMT+02:00) Helsinki, Kyiv, Riga, Sofia, Tallinn, Vilnius; 125
(GMT+02:00) Harare, Pretoria; 140
(GMT-05:00) Indiana (East); 40
(GMT+01:00) West Central Africa; 113
(GMT-08:00) Pacific Time (US & Canada); 4
(GMT+05:00) Islamabad, Karachi; 184
(GMT+11:00) Solomon Is., New Caledonia; 280
(GMT+03:00) Kuwait, Riyadh; 150
(GMT+08:00) Taipei; 220
(GMT-03:00) Montevideo; 74
(GMT+09:30) Adelaide (Commonwealth Games 2006); 251
(GMT+10:00) Hobart (Commonwealth Games 2006); 266
(GMT+05:30) Sri Jayawardenepura; 200
(GMT+04:00) Baku; 169
(GMT+05:30) Chennai, Kolkata, Mumbai, New Delhi; 190
(GMT-06:00) Central Time (US & Canada); 20
(GMT-07:00) Mountain Time (US & Canada); 10
(GMT-06:00) Saskatchewan; 25
(GMT-04:00) Cuiaba; 58
(GMT-04:00) Asuncion; 59
(GMT+01:00) Windhoek; 141
(GMT-04:00) Santiago; 56
(GMT+05:45) Kathmandu; 193
Bahia Standard Time; 71
(GMT) Monrovia, Reykjavik; 90
Discover more from Debajit's Power Apps & Dynamics 365 Blog
Subscribe to get the latest posts sent to your email.