school clock code

mail@pastecode.io avatar
unknown
javascript
2 years ago
1.1 kB
0
Indexable
Never
function showClass() {
  var classes = JSON.parse(localStorage.getItem("classes"));
  
  var now = new Date();
  var classTimes = []

  console.log(classes);

  for(var i = 0; i < classes.length; i++){
    var classesDate = new Date(classes[i]["realTime"]);
    var between = now.getTime() - classesDate.getTime();
    if (between < 0) {
        classTimes.push(between);
    }
}


var currentClass = Math.max(...classTimes);
var currentClassDate = new Date(currentClass);
var classIndex = classes.map(function(e) { return e.realTime; }).indexOf(currentClassDate);
var currentDisplay = document.getElementById("current");

var classEnd = new Date(classes[Math.abs(classIndex)]["realTime"]);
var now = new Date();
var timeBetween = Math.abs(now.getTime() - classEnd.getTime());

// Convert to min/sec

var minutes = Math.floor((timeBetween % 36e5) / 6e4);
var seconds = ((timeBetween % 6e4) / 1000).toFixed(0);
currentDisplay.innerHTML = classes[Math.abs(classIndex)]["name"] + " - ";
currentDisplay.innerHTML += `${minutes}:${(seconds < 10 ? "0" : "")}${seconds}`;
}