Solved

In time range again...

Posted on 2011-09-23
5
196 Views
Last Modified: 2012-05-12
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 http://www.experts-exchange.com/Programming/Languages/Scripting/JavaScript/Q_27320332.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?
0
Comment
Question by:EddieShipman
  • 3
  • 2
5 Comments
 
LVL 2

Accepted Solution

by:
WizardOfOgz earned 500 total points
ID: 36588415
You want to use UTC when comparing times.

d1 = Date.UTC(2011,8,25,6,30); // Please check that the time was adjusted for UTC properly
d2 = Date.UTC(2011,8,25,11,0);

currentTime = new Date();
  currentTimeMS = currentTime.getTime();
  if ( d1 < currentTimeMS && d2 > 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

0
 
LVL 26

Author Comment

by:EddieShipman
ID: 36588449
6:30 and 11:00 ??
0
 
LVL 26

Author Comment

by:EddieShipman
ID: 36588462
Sorry, GMT...
0
 
LVL 2

Expert Comment

by:WizardOfOgz
ID: 36588564
@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.
0
 
LVL 2

Expert Comment

by:WizardOfOgz
ID: 36588576
Yep, GMT ;-)
0

Featured Post

Master Your Team's Linux and Cloud Stack!

The average business loses $13.5M per year to ineffective training (per 1,000 employees). Keep ahead of the competition and combine in-person quality with online cost and flexibility by training with Linux Academy.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Avoid defining the variables in the global scope; trying to define them in a local function scope. Because:   • Look-up is performed every time a variable is accessed.   • Variables are resolved backwards from most specific to least specific scope…
When you need to keep track of a simple list of numbers or strings, the Array object is your most direct tool.  As we saw in my earlier EE Article (http://www.experts-exchange.com/A_3488.html), typical array handling might look like this: (CODE) B…
The viewer will learn the basics of jQuery, including how to invoke it on a web page. Reference your jQuery libraries: (CODE) Include your new external js/jQuery file: (CODE) Write your first lines of code to setup your site for jQuery.: (CODE)
The viewer will learn the basics of jQuery including how to code hide show and toggles. Reference your jQuery libraries: (CODE) Include your new external js/jQuery file: (CODE) Write your first lines of code to setup your site for jQuery…

770 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question