-4

Im trying to do a fancy clock with HTML. But there is a part where I want to display like00 : 00. But returns NaN. Here is the code:

var update;

function updateFunc() {
  update = setInterval(myDate, 1000);
}

function myDate() {  
    var newDate = new Date();
    document.getElementById("myDiv").innerHTML  = newDate;
}

function updateClock() {
  var update = setInterval(clockTime, 1000);
}

function clockTime() {
  var  hours = (new Date()).getHours();
  var  minutes = (new Date()).getMinutes(); 
    var  seconds = (new Date()).getSeconds();
  document.getElementById("clockMins").innerHTML = minutes + ":" + seconds; //this is my problem. 
}

updateFunc();
updateClock();
<div id="myDiv">#myDiv</div>
<div id="clockMins"></div>

Ps. How I can display the integers in two digits. Ex. 00, 01. 02.

traktor
  • 17,588
  • 4
  • 32
  • 53
Caroliner
  • 1
  • 6

1 Answers1

-4

var update; 
function n(n){
    return n > 9 ? "" + n: "0" + n;
}
function updateFunc() { 
    update = setInterval(myDate, 1000); 
} 

function myDate() { 
    var newDate = new Date(); 
    document.getElementById("myDiv").innerHTML = newDate; 
} 

function updateClock() { 
    var update = setInterval(clockTime, 1000); 
} 

function clockTime() { 
    var hours = (new Date()).getHours(); 
    var minutes = (new Date()).getMinutes(); 
    var seconds = (new Date()).getSeconds(); 
    var seperator = ':'; 
    document.getElementById("clockMins").innerHTML = n(minutes) + ":"+ n(seconds); 
}
clockTime();
updateClock();
<div id="clockMins"></div>
Caroliner
  • 1
  • 6