Time zone values in Dataverse
The “Time Zone” field (usually a lookup or option set) typically stores a TimeZoneCode, which corresponds to Microsoft’s TimeZoneDefinition table.
Datetime conversion logic
JavaScript itself cannot convert UTC to a specific Microsoft time zone ID directly — but you can use the Xrm.WebApi to retrieve the time zone offset and apply it.
Here’s a general approach using JS Web Resource:
1.Get the user’s selected Time Zone Code (integer)
Assume this is stored in a field like new_timezone.
var timeZoneCode = formContext.getAttribute("new_timezone").getValue(); // TimeZoneCode (int)
Get the UTC datetime - var utcDate = formContext.getAttribute("new_utc_datetime").getValue();
Call LocalTimeFromUtcTime function = Dataverse exposes a function that you can call using Xrm.WebApi.online.execute:
function convertUtcToLocal(utcDate, timeZoneCode) {
var request = {
UtcTime: utcDate,
TimeZoneCode: timeZoneCode,
getMetadata: function () {
return {
boundParameter: null,
parameterTypes: {
UtcTime: {
typeName: "Edm.DateTimeOffset",
structuralProperty: 1
},
TimeZoneCode: {
typeName: "Edm.Int32",
structuralProperty: 1
}
},
operationType: 0,
operationName: "LocalTimeFromUtcTime"
};
}
};
Xrm.WebApi.online.execute(request).then(
function (response) {
if (response.ok) {
response.json().then(function (result) {
var localDateTime = new Date(result.LocalTime);
console.log("Converted time:", localDateTime);
// You can now use this localDateTime as needed
});
}
},
function (error) {
console.error("Error converting time:", error.message);
}
);
}
Combine it =
var utcDate = formContext.getAttribute("new_utc_datetime").getValue();
var timeZoneCode = formContext.getAttribute("new_timezone").getValue();
if (utcDate && timeZoneCode) {
convertUtcToLocal(utcDate, timeZoneCode);
}
Notes:
- The TimeZoneCode is not always obvious — it maps to the TimeZoneDefinition entity, which contains values like “Pacific Standard Time” etc.
- You can also retrieve a list of time zones via TimeZoneDefinition if needed.
- This will work in model-driven apps where the JS is hooked into a form event or ribbon button.