M21
M21

Reputation: 343

show/hide div during business hours, mix with UTC?

UPDATE

I have added a current working fiddle, I feel like it should be correct, but it doesn't seem to work.

https://jsfiddle.net/bill9000/yadk6sja/2/

original question:

what I have is some code to show/hide div (basically show div during certain business hours) - is there an easy way to make this use a calculate from UTC... so that the show/hide time is fixed on a certain timezone. eg. 6pm EST instead of 6pm users time... here's my current code:

var d = new Date();
var dayOfWeek = d.getDay();
var hour = d.getHours();
var mins = d.getMinutes();
var status = 'open';

if (dayOfWeek !== 6 && dayOfWeek !== 0 && hour >= 03 && hour < 15){
//if (hour=='10' && mins < '30'){
   // status = 'closed';
 // }else{
   status = 'open';
 // }
}else{
status = 'closed';

if (status=='open') {
$b('.orderby').show();
}else{
$b('.orderby').hide();
}

also, I have some other JavaScript that's getting the UTC diff:

function ShowTime()  {
 var now = new Date();
 var diff = now.getTimezoneOffset() / 60; //the difference between local PC and UTC
 diff = diff - 4; //the difference between UTC and EST

  var hrs = 18-(now.getHours()+diff); //18 is the target hour

any way of making the show/hide work for the specific time?

Upvotes: 0

Views: 288

Answers (1)

Kevin Pe&#241;a
Kevin Pe&#241;a

Reputation: 772

Date() objects are already UTC, when you use d.getDay() or d.getHours(), the local timezone is applied on the fly.

You just have to use d.getUTCDay(), d.getUTCHours(), etc. to prevent this

Upvotes: 1

Related Questions