Skip to content

Instantly share code, notes, and snippets.

@stanwmusic
Created February 10, 2022 00:35
Show Gist options
  • Save stanwmusic/46639404d58538a16638044367b33351 to your computer and use it in GitHub Desktop.
Save stanwmusic/46639404d58538a16638044367b33351 to your computer and use it in GitHub Desktop.
iOS-Like Digital Clock
<div class="time">
<span class="hms"></span>
<span class="ampm"></span>
<br>
<span class="date"></span>
</div>
function updateTime() {
var dateInfo = new Date();
/* time */
var hr,
_min = (dateInfo.getMinutes() < 10) ? "0" + dateInfo.getMinutes() : dateInfo.getMinutes(),
sec = (dateInfo.getSeconds() < 10) ? "0" + dateInfo.getSeconds() : dateInfo.getSeconds(),
ampm = (dateInfo.getHours() >= 12) ? "PM" : "AM";
// replace 0 with 12 at midnight, subtract 12 from hour if 13–23
if (dateInfo.getHours() == 0) {
hr = 12;
} else if (dateInfo.getHours() > 12) {
hr = dateInfo.getHours() - 12;
} else {
hr = dateInfo.getHours();
}
var currentTime = hr + ":" + _min + ":" + sec;
// print time
document.getElementsByClassName("hms")[0].innerHTML = currentTime;
document.getElementsByClassName("ampm")[0].innerHTML = ampm;
/* date */
var dow = [
"Sunday",
"Monday",
"Tuesday",
"Wednesday",
"Thursday",
"Friday",
"Saturday"
],
month = [
"January",
"February",
"March",
"April",
"May",
"June",
"July",
"August",
"September",
"October",
"November",
"December"
],
day = dateInfo.getDate();
// store date
var currentDate = dow[dateInfo.getDay()] + ", " + month[dateInfo.getMonth()] + " " + day;
document.getElementsByClassName("date")[0].innerHTML = currentDate;
};
// print time and date once, then update them every second
updateTime();
setInterval(function() {
updateTime()
}, 1000);
body {
font-family: "Work Sans", sans-serif;
display: grid;
place-items: center;
height: 100vh;
margin: 0;
}
.time {
background: #222;
color: #fff;
padding: 8px;
text-align: center;
width: 300px;
}
.hms {
font-size: 48pt;
font-weight: 200;
}
.ampm {
font-size: 12pt;
}
.date {
font-size: 10pt;
}
<link href="https://fonts.googleapis.com/css?family=Work+Sans:200,300" rel="stylesheet" />
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment