the-glorious-startpage/js/clock.js

40 lines
801 B
JavaScript
Raw Normal View History

2020-06-07 09:18:09 +02:00
const clock = document.getElementById("clock")
2020-06-04 01:50:05 +02:00
// Append 0 before time elements if less hour's than 10
2020-06-04 01:51:25 +02:00
const appendZero = (k) => {
2020-06-04 01:50:05 +02:00
if (k < 10) {
return "0" + k;
} else {
return k;
}
}
2020-06-04 01:51:25 +02:00
// Update/Set clock
const currentTime = () => {
2020-06-04 01:50:05 +02:00
// Date object
2020-06-07 09:18:09 +02:00
const date = new Date();
2020-06-04 01:50:05 +02:00
// Set hour, minute
2020-06-07 09:18:09 +02:00
let hour = date.getHours();
let min = date.getMinutes();
let midDay = "AM";
2020-06-04 01:50:05 +02:00
// Assigning
midDay = (hour >= 12) ? "PM" : "AM";
hour = (hour == 0) ? 12 : ((hour > 12) ? (hour - 12): hour);
hour = appendZero(hour);
min = appendZero(min);
// Update clock id element
clock.innerText = hour + ":" + min + " " + midDay;
// Recursion
// Call itself if 1 second has passed.
// TOKIWO TOMAREEEE!
2020-06-07 09:18:09 +02:00
const t = setTimeout(currentTime, 1000);
2020-06-04 01:50:05 +02:00
}
2020-06-04 01:51:25 +02:00
// Start clock
2020-06-04 01:50:05 +02:00
window.onload = currentTime();