Lesson 3 - Learning construction - Step 2

by nathanesa

HTML

<p id='clock'></p>

CSS

/* Google font */
@import url('https://fonts.googleapis.com/css2?family=Orbitron&display=swap');

body {
  background-color: slategrey;
  text-align: center;
}

#clock {
  padding: 10px;
  background-color: black;
  width: 30%;
  margin: auto;
  color: red;
  font-size: 60px;
  font-family: 'Orbitron', sans-serif;
  border: 20px solid tan;
  border-radius: 10px;
}

JavaScript

// Update the minutes and hours.
function updateClock() {
  var today = new Date();
  var h = today.getHours();
  var m = today.getMinutes();
  m = formatMinutes(m);  
  document.getElementById('clock').innerHTML = h + ':' + m;
  setTimeout(updateClock, 1000);
}

// Add a 0 to the front of the minutes if less than 10.
function formatMinutes(minutes) {
  if (minutes < 10) {
    let formattedMinutes = '0' + minutes;
    return formattedMinutes
  }
  return minutes;
}

updateClock();