Solved

In time range again...

Posted on 2011-09-23
5
183 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
Comment Utility
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
Comment Utility
6:30 and 11:00 ??
0
 
LVL 26

Author Comment

by:EddieShipman
Comment Utility
Sorry, GMT...
0
 
LVL 2

Expert Comment

by:WizardOfOgz
Comment Utility
@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
Comment Utility
Yep, GMT ;-)
0

Featured Post

Free Trending Threat Insights Every Day

Enhance your security with threat intelligence from the web. Get trending threat insights on hackers, exploits, and suspicious IP addresses delivered to your inbox with our free Cyber Daily.

Join & Write a Comment

JavaScript can be used in a browser to change parts of a webpage dynamically. It begins with the following pattern: If condition W is true, do thing X to target Y after event Z. Below are some tips and tricks to help you get started with JavaScript …
International Data Corporation (IDC) prognosticates that before the current the year gets over disbursing on IT framework products to be sent in cloud environs will be $37.1B.
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…

772 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

11 Experts available now in Live!

Get 1:1 Help Now