1

This seemed fairly trivial but I might be over-thinking it.

I would like to render my chat widget between 9AM(PST) and 5PM(PST) mon-fri

Using new Date() always puts time into the browsers time zone. Basically i need to instantiate a date in PST and check if between days and hours.

var d = new Date();
var day = d.getDay();
var hour = d.getHours();

if (day > 0 && day < 6 && hour > 9 && hour < 17) {
  renderChatWidget($('#chat-widget')): 
}

I think this is incorrect because it uses the browser time, so if its 9:30AM in London then PST time would be like 2am and it would still render the chat widget...

Govind Samrow
  • 9,981
  • 13
  • 53
  • 90
Blair Anderson
  • 19,463
  • 8
  • 77
  • 114
  • 3
    Use momentJS timezone – EvgenyKolyakov Jul 18 '17 at 16:43
  • @EvgenyKolyakov do you have a fiddle or codepen showing this? – Blair Anderson Jul 18 '17 at 16:45
  • 1
    If you don't want an external library, you can also use UTC methods and convert your office times into UTC within the script: [`getUTCDate()`](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Date/getUTCDate) [`getUTCDay()`](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Date/getUTCDay) [`getUTCHours()`](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Date/getUTCHours). You'd still have to account for daylight savings yourself though, so using momentJS would be easier IMO. – Patrick Roberts Jul 18 '17 at 16:49
  • @PatrickRoberts i'm fine with an external library. but haven't found a canonical way to do this with moment either. – Blair Anderson Jul 18 '17 at 16:51
  • This answer might help you: https://stackoverflow.com/questions/15141762/how-to-initialize-javascript-date-to-a-particular-timezone – Matus Jul 18 '17 at 16:55

2 Answers2

0

function calcTime(city, offset) {
    // create Date object for current location
    var d = new Date();

    // convert to msec
    // subtract local time zone offset
    // get UTC time in msec
    var utc = d.getTime() + (d.getTimezoneOffset() * 60000);

    // create new Date object for different city
    // using supplied offset
    var nd = new Date(utc + (3600000*offset));

    // return time as a string
    return "The local time for "+ city +" is "+ nd.toLocaleString();
}

document.getElementById('title').innerHTML=(calcTime('California', '-7'));
<h1 id="title">Time Zone Example</h1>

This is from this post and I just turned it into a snippet to see it work. You can then use the modified (correct) date through your tests using the correct time zone.

Ryan
  • 69
  • 7
0

You can use UTC time.

var current_time = new Date;
var utc_time = Date.UTC(
    current_time.getUTCFullYear(),
    current_time.getUTCMonth(),     
    current_time.getUTCDate() , 
    current_time.getUTCHours(), 
    current_time.getUTCMinutes(), 
    current_time.getUTCSeconds(), 
    current_time.getUTCMilliseconds());
  • can you reformat to show how i can calculate between 9-5 in PST? – Blair Anderson Jul 18 '17 at 17:08
  • `var current_time = new Date; var utc_time = new Date( current_time.getUTCFullYear(), current_time.getUTCMonth(), current_time.getUTCDate() , current_time.getUTCHours(), current_time.getUTCMinutes(), current_time.getUTCSeconds(), current_time.getUTCMilliseconds()); console.log(new Date( utc_time.getTime() -(7*60*60*1000))); ` This will give you current PST -- UTC -7 hours – user2258948 Jul 18 '17 at 18:19