显示带有字符串的整数将返回NaN

时间:2019-05-04 02:41:21

标签: javascript html

我正在尝试用HTML做花哨的时钟。但是有一部分我想显示为00 : 00。但是返回NaN。 这是代码:

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。如何显示两位整数。例如00,01. 02.

1 个答案:

答案 0 :(得分:-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>