title: Javascript Hours of Operation check date and time with getTimezoneOffset
body: <p><strong>Updated</strong>: See below original post.</p>
  
  <p><strong>Start original post</strong><br>
  Working on changing my client's chat button to an "offline mode" if a user visits the site outside the chat hours of operation.</p>
  
  <p>Hours of operation are 9am-5pm Eastern Time (American, not Australian) Monday through Friday. When I say Eastern Time, it does need to take into account for Winter/Summer months for EST/EDT.</p>
  
  <p>EST is UTC - 5 during winter months (1st Sunday in November until 2nd Sunday in March)<Br />
  EDT is UTC - 4 during summer months (2nd Sunday in March until 1st Sunday in November)</p>
  
  <p>To avoid problems (visitor might not have computer time set correctly) I opted to use <code>getUTCDay()</code> and <code>getUTCHours()</code> instead of <code>getDay()</code> and <code>getHours()</code>. </p>
  
  <p>To convert the UTC time into EST/EDT, I subtracted <code>getTimezoneOffset()</code> from <code>getUTCHours()</code> (dividing <code>getTimezoneOffset()</code> by 60 to get hours instead of minutes first). The reason I went with this method is because according to the <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Date/getTimezoneOffset" rel="nofollow">Spec</a> <code>getTimezoneOffset()</code> is not a constant and takes into account Daylight Saving Time.</p>
  
  <p>All seems well (I think) until the if statements (which is where I believe I went wrong)<br /><br />
  Fiddle link: <a href="http://jsfiddle.net/TLJsv/" rel="nofollow">http://jsfiddle.net/TLJsv/</a><br /><Br />
  <b>Javascript</b><br /></p>
  
  <pre><code>function chatOffline() {
      var d = new Date();
      var currentMonth = d.getUTCMonth();
      var currentDay = d.getUTCDay();
      var currentHours = d.getUTCHours();
      var currentMinutes = d.getUTCMinutes();
      var currentSeconds = d.getUTCSeconds();
      var offSet = d.getTimezoneOffset() / 60;
      var offMode = function offMode() {
          jQuery('.initChat').css('background-position', '-68px 0px'); //switches sprite to offline position
          jQuery('.initChat').css('cursor', 'context-menu'); //changes cursor so button appears to be not clickable
      };
      //set UTC time to either EST or EDT
      currentHours -= offSet;
  
      //check if day is between Mon-Fri
      if (currentDay &lt; 1) {
          offMode(); // It's Sunday
      } else if (currentDay &gt; 5) {
          offMode(); // It's Saturday
      } else {
          //check if time is earlier than 9am 
          if (currentHours &lt; 9) {
              offMode(); // It's before 9am ET
          } else if
          //check if time is later than 5pm
          (currentHours &gt; 17) {
              offMode(); // It's after 5pm ET
          } else if
          //check if it is 5pm
          (currentHours == 17) {
              //if it's past 5pm, even by a second, go offline
              if (currentSeconds &gt; 0) {
                  offMode(); // It's after 5pm ET
              }
          }
      }
  }
  chatOffline();
  </code></pre>
  
  <p>Fiddle link: <a href="http://jsfiddle.net/TLJsv/" rel="nofollow">http://jsfiddle.net/TLJsv/</a></p>
  
  <p>During testing I changed the numbers in lines 23, 27, and 31 (currently the number 9 and number 17) to different values to confirm the chat link goes to offline mode. It doesn't work every time. I think the problem is with the if statement on line 23 (which checks if time is earlier than 9am). The next two seem to work fine.</p>
  
  <p>Any help or improvements on this would be greatly appreciated!
  <br><strong>End original post</strong><br></p>
  
  <p><br><br>
  <strong>Update</strong><br>
  I realized I had my last 2 <code>else if</code>s in wrong order, I've updated the fiddle <a href="http://jsfiddle.net/TLJsv/3/" rel="nofollow">http://jsfiddle.net/TLJsv/3/</a></p>