Skip to content

Instantly share code, notes, and snippets.

@marr75
Created January 16, 2021 16:51
Show Gist options
  • Save marr75/042c824ae5430b62f51382236baaed38 to your computer and use it in GitHub Desktop.
Save marr75/042c824ae5430b62f51382236baaed38 to your computer and use it in GitHub Desktop.
Finished: Digital Clock In JavaScript
<div id="MyClockDisplay" class="clock" onload="showTime()"></div>
function showTime(){
var date = new Date();
var h = date.getHours(); // 0 - 23
var m = date.getMinutes(); // 0 - 59
var s = date.getSeconds(); // 0 - 59
var session = "AM";
if(h == 0){
h = 12;
}
if(h > 12){
h = h - 12;
session = "PM";
}
h = (h < 10) ? "0" + h : h;
m = (m < 10) ? "0" + m : m;
s = (s < 10) ? "0" + s : s;
var time = h + ":" + m + ":" + s + " " + session;
document.getElementById("MyClockDisplay").innerText = time;
document.getElementById("MyClockDisplay").textContent = time;
setTimeout(showTime, 1000);
}
showTime();
body {
background: black;
}
.clock {
position: absolute;
top: 50%;
left: 50%;
transform: translateX(-50%) translateY(-50%);
color: #17D4FE;
font-size: 60px;
font-family: Orbitron;
letter-spacing: 7px;
}
<link href="https://fonts.googleapis.com/css?family=Orbitron" rel="stylesheet" />
<link href="https://fonts.googleapis.com/css?family=Aldrich" rel="stylesheet" />
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment