Solved

In time range again...

Posted on 2011-09-23
5
188 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

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

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

I've been trying to accomplish this for a while and it just struck me yesterday how to accomplish this task. I have done searches all over the internet looking for ways to email pages from my applications and finally I have done it!!! Every single s…
Introduction HTML checkboxes provide the perfect way for a web developer to receive client input when the client's options might be none, one or many.  But the PHP code for processing the checkboxes can be confusing at first.  What if a checkbox is…
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…

911 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

Need Help in Real-Time?

Connect with top rated Experts

24 Experts available now in Live!

Get 1:1 Help Now