Working...
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:
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;
// get first Sunday of any Month
var firstSunday = '';
if (month >= 10)
firstSunday = new Date(string.concat(year, '-', month, '-01'));
else
firstSunday = new Date(string.concat(year, '-0', month, '-01'));
var dayOfWeek = firstSunday.getDay();
if (dayOfWeek == 0)
firstDateofSunday = 1;
else
firstDateofSunday = 7 - (dayOfWeek - 1);
// return for DST Eligibility
if (month >= 3 && day < firstDateofSunday) return true;
if (month == 3 && day >= firstDateofSunday) return true;
if (month < 4 || month > 10) return true;
if (month > 3 && month < 10) return false;
return false;
}
// returns date and time YYYY-MM-DD HH:MM:SS
function sc_convertUTC2AEST(utcDate) {
var inDate = new Date(utcDate);
var response = '';
if (!isNaN(inDate)) {
if (isDaylightSavingsInEffect(inDate)) {
inDate.setHours(inDate.getHours() + 11);
string = '';
response = string.concat(inDate.getFullYear(), '-', ( '0' + (inDate.getMonth() + 1)).slice(-2), '-', ( '0' + inDate.getDate()).slice(-2), ' ', ( '0' + inDate.getHours()).slice(-2), ':', ( '0' + inDate.getMinutes()).slice(-2), ':', ( '0' + inDate.getSeconds()).slice(-2));
} else {
inDate.setHours(inDate.getHours() + 10);
string = '';
response = string.concat(inDate.getFullYear(), '-', ('0' + (inDate.getMonth() + 1)).slice(-2), '-', ('0' + inDate.getDate()).slice(-2), ' ', ('0' + inDate.getHours()).slice(-2), ':', ('0' + inDate.getMinutes()).slice(-2), ':', ('0' + inDate.getSeconds()).slice(-2));
}
}
return response;
}
Detailed screenshots:
TBD
No comments:
Post a Comment