when I change the time on my laptop or PC, the countdown time also changes, how do I solve the problem with the code below? thank you master.
var end_of_time = new Date("2022-01-30 10:00:00").getTime();
var x_user = setInterval(function() {
var time_now = new Date().getTime();
var distance = end_of_time - time_now;
var hours = Math.floor((distance % (1000 * 60 * 60 * 24)) / (1000 * 60 * 60));
var minutes = Math.floor((distance % (1000 * 60 * 60)) / (1000 * 60));
var seconds = Math.floor((distance % (1000 * 60)) / 1000);
document.getElementById("countdown").innerHTML = "Remaining " + hours + "h "
+ minutes + "m " + seconds + "s ";
if (distance < 0) {
document.getElementById("countdown").innerHTML = "Expired";
}
}, 1000);
Don't rely on javascript for date calculations when you run in the browser.
Think of rendering this page from the server side.
Or, if you are running an application in the browser (client-side), think of exposing a REST API to get the time from the server and proceed with calculations in UI.