Skip to content

Instantly share code, notes, and snippets.

@GeekBoySupreme
Created May 19, 2025 08:58
Show Gist options
  • Save GeekBoySupreme/7c81e45807762a4add6a7e25eb64b6f5 to your computer and use it in GitHub Desktop.
Save GeekBoySupreme/7c81e45807762a4add6a7e25eb64b6f5 to your computer and use it in GitHub Desktop.
Script to dynamically adapt timezones
var times = document.getElementsByClassName("supertime");
for (var i = 0; i < times.length; i++) {
var timezone = times[i].dataset.timezone;
var timeoffset = calculatetimeoffset(timezone.substring(4));
var flag = "add";
var giventime = times[i].innerHTML;
if (timezone.charAt(3) == "+") {
flag = "subtract";
}
giventime = absolutetime(giventime);
var utctime;
if (flag == "add") {
utctime = giventime + timeoffset;
} else {
utctime = giventime - timeoffset;
}
var utcoffset = new Date().getTimezoneOffset();
var adjustedtime = utctime - utcoffset;
times[i].innerHTML = formatTime(adjustedtime);
}
function calculatetimeoffset(timestring) {
var time_division = timestring.split(":");
var offset;
if (time_division.length > 1) {
offset = parseInt(time_division[0]) * 60 + parseInt(time_division[1]);
} else {
offset = parseInt(time_division[0]) * 60;
}
return offset;
}
function absolutetime(timestring_2) {
var time_division_1 = timestring_2.split(":");
var absolute;
if (time_division_1.length > 1) {
absolute = parseInt(time_division_1[0]) * 60 + parseInt(time_division_1[1]);
} else {
absolute = parseInt(time_division[0]) * 60;
}
return absolute;
}
function formatTime(settime) {
var hours = Math.floor(settime / 60);
var minutes = settime % 60;
newtime = hours + ":" + minutes;
return newtime;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment