function milliclock() {
// milliclock
function addZero(x, n) {
while (x.toString().length < n) {
x = "0" + x;
}
return x;
}
var tick;
var fullDate = new Date();
var twoDigitMonth = ((fullDate.getMonth().length + 1) === 1) ? (fullDate.getMonth() + 1) : +(fullDate.getMonth() + 1);
var currentDate = fullDate.getDate() + "/" + twoDigitMonth + "/" + fullDate.getFullYear();
var ch = fullDate.getHours(); // current hours
var cm = fullDate.getMinutes(); // current minutes
var cs = fullDate.getSeconds(); // current seconds
var ms = fullDate.getMilliseconds(); // current milliseconds
ch = addZero(ch, 2);
cm = addZero(cm, 1);
cs = addZero(cs, 1);
ms = addZero(fullDate.getMilliseconds(), 3);
// 00/0/000:0:00:00:000:PM
var clock_string = "<span class='currentDate'>" + currentDate + ":" + "</span><span class='currentTime'>" + (ch > 12 ? ch - 12 : ch) + ":" + (cm < 10 ? "0" : "") + cm + ":" + (cs < 10 ? "0" : "") + cs + ":" + (ms < 1000 ? "" : "") + ms + ":" + "</span><span class='ampm' style='text-transform:uppercase;'>" + (ch >= 12 ? "PM" : "AM") + "</span>";
document.getElementById('milli_clock').innerHTML = clock_string;
tick = setTimeout(function() {
milliclock();
}, 50);
}
// initialize milliclock
window.addEventListener ("load", milliclock);
simple digital clock with current date up to milliseconds
Be the first to comment
You can use [html][/html], [css][/css], [php][/php] and more to embed the code. Urls are automatically hyperlinked. Line breaks and paragraphs are automatically generated.