Roll20 uses cookies to improve your experience on our site. Cookies enable you to enjoy certain features, social sharing functionality, and tailor message and display ads to your interests on our site and others. They also help us understand how our site is being used. By continuing to use our site, you consent to our use of cookies. Update your cookie preferences .
×
Create a free account

[Question] System Time

1368084981
Konrad J.
Pro
API Scripter
Is it possible to get the users system time or even the servers for the purpose of creating stop watches, timers, etc?  RIght now I'm just using setInterval and setTimeout and things are working well, but just thought I'd ask.
You can use the Javascript Date object to access the server's time information. For example, the Unix epoch timestamp (in milliseconds) can be accessed by doing: new Date().getTime() More info is here:&nbsp; <a href="https://developer.mozilla.org/en-US/docs/JavaScript/Reference/Global_Objects/Date" rel="nofollow">https://developer.mozilla.org/en-US/docs/JavaScript/Reference/Global_Objects/Date</a> Typically in Javascript to work with times you just use that timestamp value, and then you can for example subtract one from the other and find out the difference in milliseconds between two times. var then = new Date().getTime() setTimeout(function() { var now = new Date().getTime(); var difference = now - then; // would be ~1000 which is 1000ms = 1 second }, 1000);
In general you wouldn't want to rely on a user's clock for anything, as it is inherently unreliable (you'd be shocked at how many people's computers are set to the incorrect time...). Better to rely on the server if you're wanting to know the current time or if you want to keep track of how much time has passed.
1368114360
Konrad J.
Pro
API Scripter
Riley D. said: You can use the Javascript Date object to access the server's time information. For example, the Unix epoch timestamp (in milliseconds) can be accessed by doing: new Date().getTime() More info is here:&nbsp; <a href="https://developer.mozilla.org/en-US/docs/JavaScript/Reference/Global_Objects/Date" rel="nofollow">https://developer.mozilla.org/en-US/docs/JavaScript/Reference/Global_Objects/Date</a> Typically in Javascript to work with times you just use that timestamp value, and then you can for example subtract one from the other and find out the difference in milliseconds between two times. var then = new Date().getTime() setTimeout(function() { var now = new Date().getTime(); var difference = now - then; // would be ~1000 which is 1000ms = 1 second }, 1000); Ya thats what I wanted to do, but wasn't sure we had access to the Date for some reason. &nbsp;I should have just tried it first. :)