我似乎无法显示前导零

时间:2018-03-20 12:48:55

标签: javascript html

我正在构建一个数字时钟,以便在55英寸的平面显示器上显示。我的时间显示上的前导零被忽略了,我似乎无法获得正确的组合。



window.onload = setInterval(clock, 1000);

function clock() {
  var d = new Date();

  var date = d.getDate();

  var month = d.getMonth();
  var montharr = ["Jan", "Feb", "Mar", "April", "May", "June", "July", "Aug", "Sep", "Oct", "Nov", "Dec"];
  month = montharr[month];

  var year = d.getFullYear();

  var day = d.getDay();
  var dayarr = ["Sun", "Mon", "Tues", "Wed", "Thurs", "Fri", "Sat"];
  day = dayarr[day];

  var hour = d.getHours();
  var min = d.getMinutes();
  var sec = d.getSeconds();

  document.getElementById("date").innerHTML = day + " " + date + " " + month + " " + year;
  document.getElementById("time").innerHTML =
    d.getHours() % 12 + " : " + d.getMinutes() + " : " + d.getSeconds();
}

<p id="date"></p>
<p id="time"></p>
&#13;
&#13;
&#13;

1 个答案:

答案 0 :(得分:0)

同样回答代码中的不同位置。谢谢大家

  document.getElementById("date").innerHTML=day+" "+date+" "+month+" "+year;
  document.getElementById("time").innerHTML = 

d.getHours()%12 +“:”+((d.getMinutes()&lt; 10?'0':'')+ d.getMinutes())+“:”+((d.getSeconds ()&lt; 10?'0':'')+ d.getSeconds

());     }