JavaScript Binary Clock
This is just some doodle I create just now. I have always wanted a binary watch (I have even seen it in an online shop), but I need to save money (too much spending already). So in the meantime, I think “Why don’t I just create one?” using code of course. So I write this simple script:
Put this HTML code.
<pre> <div id="full"></div> H: <span id="hour"></span> M: <span id="minute"></span> S: <span id="second"></span> </pre>
And this JavaScript code in one page.
h = document.getElementById("hour");
m = document.getElementById("minute");
s = document.getElementById("second");
f = document.getElementById("full");
function str_pad(s, n, p) {
if (p == undefined) p = '0';
while (s.length < n) {
s = p + s;
}
return s;
}
function int_to_bin(i) {
var b = '';
i = parseInt(i);
while (i > 0) {
b = (i % 2) + b;
i = parseInt(i / 2);
}
return b;
}
setInterval(function() {
var d = new Date;
h.innerHTML = str_pad(int_to_bin(d.getHours()), 6);
m.innerHTML = str_pad(int_to_bin(d.getMinutes()), 6);
s.innerHTML = str_pad(int_to_bin(d.getSeconds()), 6);
f.innerHTML = d.getHours() + ":" + d.getMinutes() + ":" + d.getSeconds();
}, 1000);
