Last active
January 14, 2022 14:12
-
-
Save GeekBoySupreme/0138335f5aeaf9d97cbf615a429bd1c4 to your computer and use it in GitHub Desktop.
Supertime.js is a micro-library that takes time in whatever zone be, and converts it to the user's local time.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<html> | |
<head> | |
<title>Supertime Demo</title> | |
</head> | |
<body> | |
<span class="supertime" data-timezone="UTC+2">12:00</span> | |
<script src="https://bit.ly/supertimejs"></script> | |
</body> | |
</html> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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