Skip to content

Instantly share code, notes, and snippets.

@andreburto
Last active August 23, 2020 23:19
Show Gist options
  • Save andreburto/19e457e4f311f803aa9aea97cf0bc0d8 to your computer and use it in GitHub Desktop.
Save andreburto/19e457e4f311f803aa9aea97cf0bc0d8 to your computer and use it in GitHub Desktop.
Basic clock display
<!DOCTYPE html>
<html>
<head>
<title>Clock Widget 3</title>
<script>
var hour = 0;
var min = 0;
var sec = 0;
var year = 0;
var month = 0
var dayte = 0;
var day = 0;
var days = ['Sun','Mon','Tue','Wed','Thr','Fri','Sat'];
var months = ['Jan','Feb','Mar','Apr','May','Jun','Jul','Aug','Sep','Oct','Nov','Dec'];
function checkSize() {
var html = document.documentElement;
var divT = document.getElementById("theTime");
var divD = document.getElementById("theDate");
// Assign font-size first to set div sizes.
divT.style.fontSize = Math.floor(html.clientHeight / 2.5) + "px";
divD.style.fontSize = Math.floor(html.clientHeight / 5) + "px";
// Calculate horizontal spacing.
var divTX = Math.floor((html.clientWidth - divT.offsetWidth) / 2);
var divDX = Math.floor((html.clientWidth - divD.offsetWidth) / 2);
// Calculate vertical spacing.
var divTY = Math.floor((html.clientHeight - (divT.offsetHeight+divD.offsetHeight)) / 2);
var divDY = divTY + divT.offsetHeight;
// Assign positions to the two div elements.
divT.style.top = divTY + "px";
divT.style.left = divTX + "px";
divD.style.top = divDY + "px";
divD.style.left = divDX + "px";
}
function updateClock() {
var time = formatSegment(hour)+":"+formatSegment(min)+":"+formatSegment(sec);
var date = days[day]+", "+months[month]+" "+dayte+", "+year;
document.getElementById("theTime").innerHTML = time;
document.getElementById("theDate").innerHTML = date;
}
function timerMinuteTick() {
var d = new Date();
hour = d.getHours();
min = d.getMinutes();
sec = d.getSeconds();
year = d.getFullYear();
month = d.getMonth();
dayte = d.getDate();
day = d.getDay();
}
function timerSecondTick() {
sec = sec + 1;
if (sec === 60) {
timerMinuteTick();
}
updateClock();
}
function formatSegment(nmbr) {
return (nmbr < 10 ? "0" : "")+nmbr;
}
function setupPage() {
timerMinuteTick();
updateClock();
checkSize();
setInterval(timerSecondTick, 1000);
}
</script>
<style>
body { margin: 0px; background-color: #000000; color: #ffffff; }
div { magin: auto; text-align: center; position: absolute; }
</style>
</head>
<body onresize="checkSize()" onload="setupPage()">
<div id="theTime"></div>
<div id="theDate"></div>
</body>
</html>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment