Skip to content

Instantly share code, notes, and snippets.

@ndsamuelson
Created March 14, 2019 23:35
Show Gist options
  • Save ndsamuelson/28f0eb71f50396e26754c04c7d9f3afd to your computer and use it in GitHub Desktop.
Save ndsamuelson/28f0eb71f50396e26754c04c7d9f3afd to your computer and use it in GitHub Desktop.
Digital Clock In JavaScript
<div id="MyClockDisplay" class="clock"></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