Solved

In time range again...

Posted on 2011-09-23
5
210 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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

Creating Instructional Tutorials  

For Any Use & On Any Platform

Contextual Guidance at the moment of need helps your employees/users adopt software o& achieve even the most complex tasks instantly. Boost knowledge retention, software adoption & employee engagement with easy solution.

Question has a verified solution.

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

This article shows how to create and access 2-dimensional arrays in JavaScript.  It includes a tutorial in case you are just trying to "get your head wrapped around" the concept and we'll also look at some useful tips for more advanced programmers. …
This article discusses how to create an extensible mechanism for linked drop downs.
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…

734 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