0

Why won't my code show "0a.004? It shows "a.0040" instead.

$(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>

By the way, I got the metric time idea from this website, and decided to put it in base-12.

2
  • Why should it show 0a.004 at first place? Commented Jan 15, 2015 at 3:31
  • how can u+1 < 12 ? because it is never true, 0 is not concatinated and you are not getting your result Commented Jan 15, 2015 at 3:37

1 Answer 1

1

Looks like you need to compare against the 12 before your toString converting to base 12. Updated snippet in the below code:

$(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>

Sign up to request clarification or add additional context in comments.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.