How can I write a script to detect when a user changes their system time in JS?
-
8Whoa you've got some serious problems if users are changing **your** system time. – Pointy Jul 29 '10 at 22:35
-
1Oh and the really interesting part of this question is what happens to already-set timeouts and interval timers? I don't know how that's implemented in real browsers; is there a low-level fine-granularity routine that ticks through the timeout/interval queue at a base frequency? – Pointy Jul 29 '10 at 22:44
7 Answers
There is no (portable) way to track a variable in JavaScript. Also, date information does not lie in the DOM, so you don't get the possibility of a DOM event being triggered.
The best you can do is to use setInterval
to check periodically (every second?). Example:
function timeChanged(delta) {
// Whatever
}
setInterval(function timeChecker() {
var oldTime = timeChecker.oldTime || new Date(),
newTime = new Date(),
timeDiff = newTime - oldTime;
timeChecker.oldTime = newTime;
if (Math.abs(timeDiff) >= 5000) { // Five second leniency
timeChanged(timeDiff);
}
}, 500);

- 88,763
- 26
- 134
- 176
-
-
@yomash, This may be a Windows or Chrome issue; I noticed it took a while to propagate changes back in time myself. It does happen, however, but just a few seconds later than you would expect. – strager Jul 30 '10 at 00:22
-
7Browsers like Safari or Mobile Safari dramatically reduce the setInterval frequency (or stop it all together) when the tab loses focus, so beware that this method can easily produce false alarms. – bendytree Jan 19 '15 at 19:59
Check in an interval function that the time has not changed too much:
function getTime() {
var d = new Date();
return d.getTime();
}
function checkTime() {
if (Math.abs(getTime() - oldtime) > 2000) { // Changed by more than 2 seconds?
alert("You changed the time!");
}
oldtime = getTime();
}
var oldtime = getTime();
setInterval(checkTime, 1000); // Check every second that the time is not off
Tested on Windows with Opera & FF and works flawlessly.

- 15,005
- 4
- 44
- 68
Don't think there is a solution to what you are asking for but you can get the users timezone offset.
new Date().getTimezoneOffset() * -1
This returns the offset in minutes from GMT. Bare in mind though this does not take DST into consideration.

- 936
- 1
- 11
- 25
-
To clarify, daylight savings time produces an effectively different time zone with a different offset. e.g. eastern time as EST (-5) vs EDT (-4). – MattH Jan 01 '20 at 21:48
var last_time = new Date().getTime();
setInterval(function() {
var time = new Date().getTime();
var offset = time - last_time;
if(offset < 0 || offset > 1500) {
// Time has been changed
}
last_time = time;
}, 1000);
In theory, this should work. It will check every second to make sure the time hasn't been changed. Note that I use 1100 milliseconds as most JS interpreters don't fire off events at exactly the time specified.
Hope this helps!

- 13,492
- 9
- 47
- 68
use performance.now()
to get duration, which will be independent of system clock
see https://developer.mozilla.org/en-US/docs/Web/API/Performance/now
var t0 = performance.now();
doSomething();
var t1 = performance.now();
console.log("Call to doSomething took " + (t1 - t0) + " milliseconds.");
And then you can compare performance.now()
elapsed with Date.now()
elapsed to see whether they are diff too much.

- 431
- 5
- 7
Do you mean if they are changing their own system time to something that is wrong? You can ask the user for their time zone and get the correct time from the server, and then compare it to the user's system time.

- 17,658
- 5
- 50
- 82
-
My page has generated events at the exact time. When the user changes his system time, then skip several events for example. – AHOYAHOY Jul 29 '10 at 22:43
You could check every 30 seconds, etc. If the new Time is off by more than 30 seconds +/- some threshold, you could do a more exhaustive comparison to determine how much it has been changed.

- 2,021
- 1
- 14
- 15