D365 CE v9.x: How to convert Date Time to current user’s timezone using latest Xrm method.
Views (1450)
Introduction
It’s always complex to convert date time to current user’s timezone.
In this blog, we will learn how to use the latest Xrm method to convert date time to the user’s current timezone.
Background Knowledge
When you see a date-time value on the browser (in debugging mode) it’s always local timezone value. Do you know how does this happen?
Let’s learn from the below diagram.
- Dynamics 365 store Date-Time in UTC format.
- When a date is retrieved using HTTP request(Web-API), it’s converted to system local timezone from UTC.
Default Behavior
Let’s understand how to convert date time value.
///If this is a custom Web Resource get Xrm object from a parent form
var Xrm = parent.Xrm;
function onLoad() {
//Let's assumed that this is date that you have revived from Web API
var currentDate = new Date("12/1/2019 05:00:00");
var timeZoneOffsetMinutes = Xrm.Utility.getGlobalContext().userSettings.getTimeZoneOffsetMinutes;
var convertedDate = convertDateFromOffset(currentDate, timeZoneOffsetMinutes);
}
function convertDateFromOffset(date, offset) {
// offset: should be in minutes
// date: should be Date Object
// create Date object for current location
var d = new Date(date);
// convert to msec
// add local time zone offset
// get UTC time in msec
var utc = d.getTime() + (d.getTimezoneOffset() * 60000);
// create new Date object for specific offset
// using supplied offset
return new Date(utc + (60000 * -offset));
}
Cheers!!! We have successfully converted date time to current user’s timezone.
This was originally posted here.

Like
Report

*This post is locked for comments