Eli Bel
Eli Bel

Reputation: 11

What's wrong with this JavaScript/jQuery code?

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.

Upvotes: 1

Views: 56

Answers (1)

Jason W
Jason W

Reputation: 13179

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>

Upvotes: 1

Related Questions