Last active
November 16, 2020 11:29
-
-
Save paulera/9a8a8c293926b1725d746b9327152d35 to your computer and use it in GitHub Desktop.
Display the current time HH:MM for a given timezone
This file contains hidden or 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> | |
<body> | |
<!-- See live at https://codepen.io/paulera/pen/BPwPNw --> | |
<p class="clock" timezone="+2"></p> | |
<p class="clock" timezone="+4"></p> | |
<p class="clock" timezone="-7"></p> | |
<script> | |
function dateToText(date) { | |
var hours = date.getHours() | |
var minutes = date.getMinutes(); | |
// var seconds = date.getSeconds(); | |
if (minutes < 10) minutes = '0'+minutes; | |
//if seconds < 10) seconds = '0'+seconds; | |
if (hours < 10) hours = '0'+hours; | |
return hours + ":" + minutes; // + ":" + seconds; | |
} | |
function updateClocks() { | |
for (var i = 0; i < window.arrClocks.length; i++) { | |
var clock = window.arrClocks[i]; | |
var offset = window.arrOffsets[i]; | |
clock.innerHTML = dateToText(new Date(new Date().getTime()+offset)); | |
} | |
} | |
function startClocks() { | |
clockElements = document.getElementsByClassName('clock'); | |
window.arrClocks = [] | |
window.arrOffsets = []; | |
var j = 0; | |
for(var i = 0; i < clockElements.length; i++) { | |
el = clockElements[i]; | |
timezone = parseInt(el.getAttribute('timezone')); | |
if (!isNaN(timezone)) { | |
var tzDifference = timezone * 60 + (new Date()).getTimezoneOffset(); | |
var offset = tzDifference * 60 * 1000; | |
window.arrClocks.push(el); | |
window.arrOffsets.push(offset); | |
} | |
} | |
updateClocks(); | |
clockID = setInterval(updateClocks, 1000); | |
} | |
setTimeout(startClocks, 100); | |
</script> |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment