1- To ensure the accuracy of your server's data-time, you can send it to the client by placing it in a hidden tag with a specific id. On the client side, you can then compare this time with the local date and time. For example, let's assume our hidden element looks like this:
<input type='hidden' value='2019/01/05 08:17Z' id='server_time'/>
You can compare this with the local date-time using the following code (inside
window.onload=function(){/*.. here ..*/}
):
// If the elapsed time relative to the server time is greater than 60 seconds ...
if((new Date() - new Date(document.getElementById("server_time").value))/1000 > 60) alert("Please correct your system date!!"); // or any other action
2- If you do not have access to the server side...
In such scenarios, you can try at least these two approaches:
a- You can utilize a free API
to get the accurate time. Consider this example:
var hr = new XMLHttpRequest();
hr.open("GET", "http://worldclockapi.com/api/json/utc/now");
hr.onreadystatechange = function(){
if(hr.readyState != 4) return;
if(hr.status != 200 && hr.status != 304) return alert("error");
if((new Date() - new Date(JSON.parse(hr.responseText).currentDateTime))/1000 > 60) alert("Please correct your system date!!");
}
hr.send();
b- Another alternative is:
(Assuming the time on your server is accurate,) you can extract the date from the header info
! To achieve this, you can trigger a simple action from your host (such as pointing to a non-existent page). Try the following implementation:
var hr = new XMLHttpRequest();
hr.open("GET", "blankpage.html");
hr.onreadystatechange = function(){
if(hr.readyState != 4) return;
if((new Date() - new Date(hr.getAllResponseHeaders().match(/date *\: *(.+)[\r\n]/i)[1]))/1000 > 50) alert("Please correct your system date!!");
};
hr.send();