3

So I have web watch what should work 24/7. It displaying digital time and current day. Nothing else.

My problem is that when time past midnight, current day won't update and I need to restart whole application.

I tried to add setTimeout for showDate() function, but it didn't help me. I was thinking about moving +1 on array of days. Is there anything more effective?

function showdate(){

    var date = new Date();
    var day = date.getDay();

    var name = ["Sunday","Monday","Tuesday","Wednesday","Thursday","Friday","Saturday"][day];

    document.getElementById("date").textContent = name;

}

showdate();

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 = 24;
    }

    /*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;
    /*var sess = session;*/
    document.getElementById("MyClockDisplay").innerText = time;
    document.getElementById("MyClockDisplay").textContent = time;
   /* document.getElementById("PM").textContent = sess;*/

    setTimeout(showTime, 1000);     

   }

showTime();

Thank you for any idea.

Cabry
  • 125
  • 12

3 Answers3

1

Your code doesn't execute itself automatically. You shall call it periodically like this :

function showdate(){

    var date = new Date();

    var day = date.getDay();

    var name = ["Sunday","Monday","Tuesday","Wednesday","Thursday","Friday","Saturday"][day];

    document.getElementById("date").textContent = name;

        console.log('show date');
}

setInterval(showdate, 1000)

This way, the client will get the new date every second (where 1000 is in milliseconds). But you can change the time to any value (keep in mind it's in ms) that fit your needs.

You also can take a look at this example.

Maxime Lafarie
  • 2,172
  • 1
  • 22
  • 41
1

function showdate(){
    var date = new Date();
    var day = date.getDay();
    var name = ["Sunday","Monday","Tuesday","Wednesday","Thursday","Friday","Saturday"][day];
    document.getElementById("date").textContent = name;

  console.log('show date');
}
setInterval(showdate, 1000)

function checkTime(i) {
    if (i < 10) {
      i = "0" + i;
    }
    return i;
}

function startTime() {
    var today = new Date();
    var h = today.getHours();
    var m = today.getMinutes();
    var s = today.getSeconds();
    // add a zero in front of numbers<10
    m = checkTime(m);
    s = checkTime(s);
    document.getElementById('time').innerHTML = h + ":" + m + ":" + s;
    t = setTimeout(function() {
      startTime()
    }, 500);
}
startTime();
<!DOCTYPE html>
<html>
<head>

</head>
<body>

<div id="date"></div>
<div id="time"></div>

</body>
</html>
Andrei Tornea
  • 108
  • 10
1

You can do:

// Show date every day at 12
function showdateEveryDayAt12() {
  setInterval(function() {
    showdate();
  }, 86400000);
}

// Show date at next 12, and call showdateEveryDayAt12()
function showdateAtNext12() {
  var now = new Date();
  var millisecondsTo12 = new Date(now.getFullYear(), now.getMonth(), now.getDate(), 12, 0, 0, 0) - now;

  setTimeout(function() {
    showdate();
    showdateEveryDayAt12();
  }, millisecondsTo12);
}

// Show date
function showdate() {
  var date = new Date();
  var day = date.getDay();
  var name = ["Sunday", "Monday", "Tuesday", "Wednesday", "Thursday", "Friday", "Saturday"][day];

  document.getElementById("date").textContent = name;
}


// Show date now, at next 12 and every day at 12
showdate();
showdateAtNext12();
<p id="date"></p>
Yosvel Quintero
  • 18,669
  • 5
  • 37
  • 46