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

Javascript Timezone question

1619218050
I'm using this little scriptlet (adapted from Wolf Thunderspirit's here ) to add a couple messages in the game chat window and API log. const APIStartup = (()=>{     const scriptName = "API Startup";     const version = '0.1';     const OuterStyleStart = `width: 75%; margin: auto; padding: 0px; border-radius: 4px;  box-shadow: 1px 1px 1px #707070;`;     const InnerStyleStart = `text-align: center; vertical-align: middle; padding: 0px 0px; margin: 0px auto; border: 1px solid #000; border-radius: 4px; color: #000; background-image: -webkit-linear-gradient(-45deg, #a7c7dc 0%,#85b2d3 100%); text-shadow: 0px 0px #222, 0px 0px #222, 0px 0px #222, 0px 0px #222 , 0px 0px #222;`;     var currentTime = new Date();     currentTime.setHours(currentTime.getTimezoneOffset()-8);     var timestamp = (new Date(currentTime).toLocaleString('en-US'));     const checkInstall = function() {           log('-=> ' + scriptName + ' v' + version + ' <=-');           setTimeout(()=>log(`API Wakeup Complete at ` +timestamp),0);           sendChat("API Startup", `/w gm <div style='${OuterStyleStart}'><div style='${InnerStyleStart}'>API Wakeup Complete</div></div>`,null,{noarchive:true});     };     log(`API Wakeup Begun...`);     sendChat("API Startup", `/w gm <div style='${OuterStyleStart}'><div style='${InnerStyleStart}'>API Starting Up</div></div>`,null,{noarchive:true});     on("ready",() => {         checkInstall();     }); })(); But for some reason the Date/Time offset that gets printed in the log isn't consistent, so I'm guessing that I am using an incorrect version of .getTimezoneOffset or .toLocaleString. By inconsistent, I mean the time that is printed will vary by several hours different than the time that is currently displayed on my computer.  Any idea what I'm doing wrong? I'm pretty sure it's something simple! I'm hoping to set it to base the offset from my computer's timezone, but if it was set to a static timezone that would work fine too (U.S Pacific timezone).
1619224381
The Aaron
Forum Champion
API Scripter
The time will be based on wherever the API server is (or possibly how it is configured), not your browser. To get it to a specific time zone you'll need to manually offset it. 
1619277641
The Aaron said: The time will be based on wherever the API server is (or possibly how it is configured), not your browser. To get it to a specific time zone you'll need to manually offset it.  Well that makes sense for why it seemed to be almost randomly changing! I thought it had to do with my VPN (but it was still happening when not using the VPN). Is there a way to force it to a specific timezone, regardless of how much of an offset from the API server it is? Looking online I'm seeing code like: .toLocaleString("en-US", {timeZone: "America/Los_Angeles"})     var currentTime = new Date();     currentTime.setHours(currentTime.getTimezoneOffset()-8);     var timestamp = (new Date(currentTime).toLocaleString("en-US", {timeZone: "America/Los_Angeles"})); But it seems as though that still doesn't work (it's still based on the API Server's time). Or is there a way to pull time in a different way so that it's not basing it on the API server? Thanks!
1619283393
The Aaron
Forum Champion
API Scripter
The date will be in UTC, so you're double correcting it by subtracting 8 and putting it in a given timezone.  if you just need the string, you can do it like this: let timestamp = (new Date()).toLocaleString("en-US", {timeZone: "America/Los_Angeles"});
1619285220
The Aaron said: The date will be in UTC, so you're double correcting it by subtracting 8 and putting it in a given timezone.&nbsp; if you just need the string, you can do it like this: let timestamp = (new Date()).toLocaleString("en-US", {timeZone: "America/Los_Angeles"}); Ayup... I was overcomplicating it. I knew it was something easy. Thanks! In case anyone else wants a little script for adding startup messages in chat and the API log: // Based on script here: <a href="https://app.roll20.net/forum/post/9379560/script-help-new-to-scripting-need-help-on-one-started#post-9412121" rel="nofollow">https://app.roll20.net/forum/post/9379560/script-help-new-to-scripting-need-help-on-one-started#post-9412121</a> const APIStartup = (()=&gt;{ &nbsp; &nbsp; const scriptName = "API Startup"; &nbsp; &nbsp; const version = '0.1'; &nbsp; &nbsp; const OuterStyleStart = `width: 75%; margin: auto; padding: 0px; border-radius: 4px;&nbsp; box-shadow: 1px 1px 1px #707070;`; &nbsp; &nbsp; const InnerStyleStart = `text-align: center; vertical-align: middle; padding: 0px 0px; margin: 0px auto; border: 1px solid #000; border-radius: 4px; color: #000; background-image: -webkit-linear-gradient(-45deg, #a7c7dc 0%,#85b2d3 100%); text-shadow: 0px 0px #222, 0px 0px #222, 0px 0px #222, 0px 0px #222 , 0px 0px #222;`; &nbsp; &nbsp; let timestamp = (new Date()).toLocaleString("en-US", {timeZone: "America/Los_Angeles"}); &nbsp; &nbsp; const checkInstall = function() { &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; log('-=&gt; ' + scriptName + ' v' + version + ' &lt;=-'); &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; setTimeout(()=&gt;log(`API Wakeup Complete at ` +timestamp),0); &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; sendChat("API Startup", `/w gm &lt;div style='${OuterStyleStart}'&gt;&lt;div style='${InnerStyleStart}'&gt;API Wakeup Complete&lt;/div&gt;&lt;/div&gt;`,null,{noarchive:true}); &nbsp; &nbsp; }; &nbsp; &nbsp; log(`API Wakeup Begun...`); &nbsp; &nbsp; sendChat("API Startup", `/w gm &lt;div style='${OuterStyleStart}'&gt;&lt;div style='${InnerStyleStart}'&gt;API Starting Up&lt;/div&gt;&lt;/div&gt;`,null,{noarchive:true}); &nbsp; &nbsp; on("ready",() =&gt; { &nbsp; &nbsp; &nbsp; &nbsp; checkInstall(); &nbsp; &nbsp; }); })();