Convert Time into User Time Using Time Zone Codes

Sending
User Review
0 (0 votes)

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.
Firstly, 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.

//Read the time zone codes of user
function setUserDateTimeZone(userId) {
//generate the fetchXml
var fetchXml = “”;

fetchXml += “”;

fetchXml += “”;

fetchXml += “”;

fetchXml += “”;

fetchXml += “”;

fetchXml += “”;

fetchXml += “”;

fetchXml += “”;

fetchXml += “”;

fetchXml += “”;

fetchXml += “”;

fetchXml += “”;

fetchXml += “”;

fetchXml += “”;

fetchXml += “”;

fetchXml += “”;
//get the result

fetchXml = “?fetchXml=” + encodeURIComponent(fetchXml);
//Fetch the user time zone information

Xrm.WebApi.retrieveMultipleRecords(‘usersettings’, fetchXml).then(function success(results) {
//create time Zone object
var timeZoneObj = {};

timeZoneObj.timezonecode = results.entities[0][“timezonecode”];

timeZoneObj.timezonebias = results.entities[0][“timezonebias”];

timeZoneObj.timezonedaylightbias = results.entities[0][“timezonedaylightbias”];

timeZoneObj.timezonedaylightday = results.entities[0][“timezonedaylightday”];

timeZoneObj.timezonedaylightminute = results.entities[0][“timezonedaylightminute”];

timeZoneObj.timezonedaylightyear = results.entities[0][“timezonedaylightyear”];

timeZoneObj.timezonedaylightsecond = results.entities[0][“timezonedaylightsecond”];

timeZoneObj.timezonedaylightdayofweek = results.entities[0][“timezonedaylightdayofweek”];

timeZoneObj.timezonedaylighthour = results.entities[0][“timezonedaylighthour”];

timeZoneObj.timezonedaylightmonth = results.entities[0][“timezonedaylightmonth”];

},
function (error) {

console.log(error.message);

});
}

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 (“+00:00”) offset value and time zone codes of the user to generateUserTime function below:

var dateValue = “2020-12-25 12:30:30+00:00”;
//convert the given time to user Time Zone time
var generateUserTime = function (dateValue, timeZoneObj) {
//get the user time zone offset value
var timeZoneOffset = findTimeZoneOffset(timeZoneObj);
if (isValid(timeZoneOffset)) {

dateValue = dateValue.split(“+00:00”);
returnnew Date(dateValue[0] + timeZoneOffset);

}
}s
//function call to get current daylight offset
function findTimeZoneOffset(timeZone) {
var actualTimeOffset = “”;
//get time zone offset value
var timeOffset = timeZone.timezonebias;
//check if daylight is present or not
if (timeZone.timezonedaylightday != 0 || timeZone.timezonedaylightminute != 0 || timeZone.timezonedaylightyear != 0 || timeZone.timezonedaylightsecond != 0 || timeZone.timezonedaylightdayofweek != 0 || timeZone.timezonedaylighthour != 0 || timeZone.timezonedaylightmonth != 0) {
//get time zone daylight value

timeOffset = (timeOffset + (timeZone.timezonedaylightbias));

}
//get actual time offset according time zone
if (isPostiveNegative(timeOffset) == -1) {

timeOffset = parseInt(timeOffset.toString().split(‘-‘)[1]);
var hours = Math.floor(timeOffset / 60);
var minutes = timeOffset % 60;

actualTimeOffset = “+” + (hours.toString().length == 1 ? “0” + hours : hours) + “:” + (minutes.toString().length == 1 ? “0” + minutes : minutes);

} elseif (isPostiveNegative(timeOffset) == 1) {
var hours = Math.floor(timeOffset / 60);
var minutes = timeOffset % 60;

actualTimeOffset = “-” + (hours.toString().length == 1 ? “0” + hours : hours) + “:” + (minutes.toString().length == 1 ? “0” + minutes : minutes);

} elseif (isPostiveNegative(timeOffset) == 0) {
var hours = Math.floor(timeOffset / 60);
var minutes = timeOffset % 60;

actualTimeOffset = “+” + (hours.toString().length == 1 ? “0” + hours : hours) + “:” + (minutes.toString().length == 1 ? “0” + minutes : minutes);

}
return actualTimeOffset;
}
//Check number is postive or not
function isPostiveNegative(num) {
return num === 0 ? num : (num > 0 ? 1 : -1);
}

Conclusion

Using user Time Zone codes, we can easily find and convert time into user time.

Leave a Reply