Search code examples
javascriptclockradix

Why isn't my output in base 8?


So I am looking to make a base8 clock. Still using twenty four 308 hours in a day, for convenience. I would have thought that converting the indivual values to base 8 in the last step before throwing them in an order and displaying them would work but I'm getting weird values out which are nowhere near the current time. It's even gave me numbers such as 9. Base8 has no 9, it has 118.

Yes I based this on clock code I found on a site, I get lazy. Maybe this is my downfall. But can somebody tell me where I am going wrong?

<!--

/*By JavaScript Kit
http://javascriptkit.com
Credit MUST stay intact for use
*/

function show2() {
  if (!document.all && !document.getElementById)
    return
  thelement = document.getElementById ? document.getElementById("tick2") : document.all.tick2
  var Digital = new Date();
  var hours = Digital.getHours();
  var minutes = Digital.getMinutes();
  var seconds = Digital.getSeconds();
  var hour8 = parseInt(hours, 8);
  var min8 = parseInt(minutes, 8);
  var sec8 = parseInt(seconds, 8);
  if (hour8 == 0)
    hour8 = 10
  var ctime = hour8 + ":" + min8 + ":" + sec8
  thelement.innerHTML = "<b style='font-size:14;color:blue;'>" + ctime + "</b>"
  setTimeout("show2()", 1000)
}
window.onload = show2
  //-->
<span id=tick2>
</span>


Solution

  • Apart from the fact that you need to do

    setTimeout(show2, 1000)
    

    without " and (), your use of parseInt is wrong, the radix is the base the string is ALREADY expressed in.

    You need to do this

    var hour8 = parseInt(hours).toString(8)
    

    See this jsFiddle