I created a simple digital clock using JavaScript to show the time on a TV screen. However, after several hours of running, I noticed that the displayed time gets off by a few seconds (around 30 or more).
Below is the code snippet I used:
getTime() {
const time = new Date();
const formatter = Intl.DateTimeFormat('de-AT', {
hour: "2-digit",
minute: "2-digit"
});
return formatter.format(time);
}
updateClock() {
this.clock = this.getTime();
}
This function is called within a setInterval method:
setInterval(() => {
this.updateClock();
}, 1000);
I'm puzzled as to why the time discrepancy occurs after some time has passed. Given that I create a new Date object each time, it should theoretically maintain accurate timekeeping.
EDIT: Upon further investigation, it appears that the inaccuracy stems from the internal clock of the TV itself, which drifts out of synchronization over extended periods (for unknown reasons). As the JavaScript DateTime relies on the TV's system time, which is incorrect, the displayed clock also becomes inaccurate.