Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

Second function isn't called in javascript using setInterval

I’m trying to make a simple live clock and a dd/mm/yy output on page,first function seems to work fine , but unfortunately the second function is not displaying on the page for some reason , tried everything but didn’t helped. I would be grateful if you could help me. Thank you in advance

setInterval(function clock() {
  var date = new Date();
  var daylist = ["Sunday", "Monday", "Tuesday", "Wednesday ", "Thursday", "Friday", "Saturday"];
  var day = date.getDay();
  var hour = date.getHours();
  var min = date.getMinutes();
  var sec = date.getSeconds();
  var Part = (hour >= 12) ? 'PM' : 'AM';
  if (hour == 0 && Part == 'PM') {
    if (minute == 0 && sec == 0) {
      hour = 12;
      Part = 'Noon'
    } else {
      hour = 12;
      Part = 'PM'
    }
  }
  if (hour > 12) {
    hour = hour - 12;
  }
  var curentday = ('<br></br>' + 'Today is: ' + daylist[day]);
  var time = ('Current time is : ' + hour + Part + ': ' + min + ': ' + sec + curentday);
  document.getElementById('time').innerHTML = time;
}, 1000);

setInterval(function display() {
  var date = new Date();
  var date = date.getDate();
  var month = date.getMonth() + 1;
  var year = date.getFullYear();
  var fullDate = ('Current date is :' + date + ':' + month + ':' + year);
  document.getElementById('x').innerHTML = fullDate;
}, 1000);
body {
  background-color: red;
}

#time {
  color: white;
  font-size: 3vw;
  font-family: Arial, Helvetica, sans-serif;
  font-weight: 900;
  text-align: center;
  margin: 20%;
}

#x {
  color: white;
  font-size: 3vw;
  font-family: Arial, Helvetica, sans-serif;
  font-weight: 900;
  text-align: center;
  margin: 50%;
}
<div id="time"></div>
<div id="x"></div>

>Solution :

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

this is a modified version of the code, i changed "date" variable to "day" to be more explicit and i changed getDate() by getDay()

setInterval(function display(){
    var date = new Date();

        var day = date.getDay();
        var month = date.getMonth() + 1;
        var year = date.getFullYear();

  var fullDate =('Current date is :' + day + ':' + month + ':' + year );
  document.getElementById('x').innerHTML = fullDate;
}, 1000);

I think this what you looking for

Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading