Link to home
Start Free TrialLog in
Avatar of Eddie Shipman
Eddie ShipmanFlag for United States of America

asked on

In time range again...

We are going to throw a message up during Paypal's planned outage this weekend to prevent customers from getting confused when they can't purchase when Paypal is down. This is the code we are currently using, kindly provided by gurvinder372 in this post https://www.experts-exchange.com/questions/27320332/Is-within-time-range.html:
  d1 = new Date();
  d1.setDate(24);
  d1.setMonth(8);
  d1.setFullYear(2011);
  d1.setHours(23);
  d1.setMinutes(30);
  
  d2 = new Date();
  d2.setDate(25);
  d2.setMonth(8);
  d2.setFullYear(2011);
  d2.setHours(4);
  d2.setMinutes(0);
  
  currentTime = new Date();
  currentTimeMS = currentTime.getTime();
  if ( d1.getTime() < currentTimeMS && d2.getTime() > currentTimeMS ) {
    alert("<b>Our payment processor has a planned outage between 11:30PM PDT Sep. 24 and 4:00AM PDT Sep. 25, 2011.<br>"+
          "If you receive this message then our provider is offline at this time.</b>");
  }

Open in new window


The problem I neglected to see lies in the fact that the outage is happening between 11:30PM PDT and 4:00AM PDT.

How do I account for the difference between the client's time zone and PDT?
ASKER CERTIFIED SOLUTION
Avatar of WizardOfOgz
WizardOfOgz

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of Eddie Shipman

ASKER

6:30 and 11:00 ??
Sorry, GMT...
Avatar of WizardOfOgz
WizardOfOgz

@EddieShipman Yes, those times were converted from PDT (which is -7 hours) to Universal Time.

Creating a date with your original code uses the client's local time, so it will vary according to the user's timezone setting.
d1 = new Date();
  d1.setDate(24);
  d1.setMonth(8);
  d1.setFullYear(2011);
  d1.setHours(23);
  d1.setMinutes(30);
  // d1 will be different for each timezone!

Open in new window


So instead we create a UTC date to avoid the timezone differences.
d1 = Date.UTC(2011,8,25,6,30); // Same no matter what the client's local timezone is

Open in new window


This all relies on the fact that
9/25/11 6:30am UTC == 9/24/11 11:30pm PDT == 9/25/11 1:30am CDT, etc.
Yep, GMT ;-)