这个JavaScript / jQuery代码有什么问题?

时间:2015-01-15 03:24:29

标签: jquery

为什么我的代码不会显示“ 0 a.004?它会显示”a.004 0 “。

$(document).ready(function() {
  /* I will eventually replace my example with this
  var d = new Date();
  var h = d.getUTCHours();
  var m = d.getUTCMinutes();
  var s = d.getUTCSeconds();
  var d = new Date(); */
  var h = 0;
  var m = 144;
  var s = 2;
  var u = (s / 864) + (m / 14.4) + (h / 0.24);
  // Tried as an example: var u = 11.060    
  u = u.toString(12);
  if (u + 1 < 12) {
    u = "0" + u;
  }
  var n = u.substring(0, 6);
  $("#time").append(n);
});
#time {
  color: #000;
  font-family: "Courier New";
  font-size: 60pt;
  text-align: center;
}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.11.1/jquery.min.js"></script>
<div id="time"></div>

顺便说一下,我从this website得到了公制时间的想法,并决定把它放在12号基地。

1 个答案:

答案 0 :(得分:1)

您需要在toString转换为基础之前与12进行比较。在以下代码中更新了代码段:

$(document).ready(function() {
  /* I will eventually replace my example with this
  var d = new Date();
  var h = d.getUTCHours();
  var m = d.getUTCMinutes();
  var s = d.getUTCSeconds();
  var d = new Date(); */
  var h = 0;
  var m = 144;
  var s = 2;
  var u = (s / 864) + (m / 14.4) + (h / 0.24);
  var n = (u + 1 < 12 ? '0' : '') + u.toString(12).substring(0, 6);
  $("#time").append(n);
});
#time {
  color: #000;
  font-family: "Courier New";
  font-size: 60pt;
  text-align: center;
}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.11.1/jquery.min.js"></script>
<div id="time"></div>