Usecase:
Here, we will demonstrate how to convert one timezone to another considering the daylight saving time.
Here, we will see UTC to Australia timezone change.
Implementation steps:
- Create 2 javascript functions
- 1st one will determine whether the datetime under DST or not. Response will be in True or False.
- 2nd one will add UTC +11.00 or UTC+10.00 depends on the daylight saving.
- Import it to OIC library
- Create an integration and feed a datetime
- Feed the input date time to javascript and get the the converted timezone from the integration.
Javascript code used: (save .js extension)
function isDaylightSavingsInEffect(dateTimeInput) {
var dateTime = new Date(dateTimeInput);
var month = dateTime.getMonth();
month = month + 1;
var year = dateTime.getFullYear();
var day = dateTime.getDate();
var hours = dateTime.getHours();
var firstDateofSunday = 0;
var flag = false;
// get first Sunday of any Month
var firstSunday = new Date(year, month - 1, 1);
var dayOfWeek = firstSunday.getDay();
if (dayOfWeek == 0) {
firstDateofSunday = 1;
} else {
firstDateofSunday = 7 - (dayOfWeek - 1);
}
// return for DST Eligibility
if (month < 4 || month > 10 || (month == 10 && day >= firstDateofSunday) || (month == 4 && day <= firstDateofSunday)) {
flag = true;
} else {
flag = false;
}
return flag;
}
// returns date and time YYYY-MM-DD HH:MM:SS
function sc_convertUTC2AEST (utcDate) {
var inDate = new Date(utcDate);
var offset = 0;
var response = '';
if (!isNaN(inDate)) {
offset = isDaylightSavingsInEffect(inDate) ? 11 : 10; // Using your function to decide offset
inDate.setHours(inDate.getHours() + offset);
string ='';
response= string.concat(inDate.getUTCFullYear(), '-', (('0' + (inDate.getUTCMonth() + 1)).slice(-2)),'-', ('0' + inDate.getUTCDate()).slice(-2),' ', ('0' +
inDate.getUTCHours()).slice(-2), ('0' + inDate.getUTCMinutes()).slice(-2), ('0' + inDate.getUTCSeconds()).slice(-2))
}
return response;
}
Detailed screenshots:
Js Codes: