0

I'm trying to write a JavaScript code that will display a certain message between two hours in a day. We're in eastern standard time but I have no problem working with universal time, because that makes writing the script much easier. So far, I have this and it works great, however, I'm completely stumped with regards to working with daylight savings time.

$(document).ready(function(){
    var todaysDate = new Date();
    var weekday = todaysDate.getDay();
    var universalhour = todaysDate.getUTCHours();

if (weekday >= 0) {
    if (weekday <= 4) {
        if (universalhour >= 14) {
            if (universalhour < 23) {
                $('div#announcements span').append('<br />Open.');
            }
        }
    }
}
if (weekday == 5) {
    if (universalhour >= 14) {
        if (universalhour < 20) {
            $('div#announcements span').append('<br />Open.');
        }
    }
}
});

Basically, the message "Open" should only display between 10am EST and 8pm EST, Sunday-Thursday and 10am EST to 4pm EST Friday.

I have no problem working with UST, I just need help figuring out a workaround for Daylight Savings Time, as this i sbeyond my field of knowledge.

henryaaron
  • 6,042
  • 20
  • 61
  • 80
  • `getDay` is not the _UTC_ version so is location dependant, perhaps you meant `getUTCDay`. – Paul S. Oct 15 '13 at 18:20
  • 1
    You can check if DST is in effect http://stackoverflow.com/questions/11887934/check-if-daylight-saving-time-is-in-effect-and-if-it-is-for-how-many-hours – jorgebg Oct 15 '13 at 18:21
  • @jorgebg that only applies to the local timezone on the client's machine. Not every place changes to _DST_ at the same time and some places don't at all. – Paul S. Oct 15 '13 at 18:24
  • @jorgebg I used this + EST offset to write my script. Post an answer and I'll accept. – henryaaron Nov 11 '13 at 20:39

2 Answers2

0

Case 1

If your computer is set to run on the Eastern Time Zone and you use this change:

var hour = todaysDate.getHours();

Then use that variable in all the tests.

That should take care of it.

Case 2

If you are writing JavaScript to run on a client machine at the end of some network/internet connection, you will need to pass something from the server to the client so the javascript can tell what the time is in the Eastern Time Zone.

So you could include the following in your web page

<script>
   // The global value is filled in by the server based on current time on your server which is
   //  running EST or can calculate it. That code might be PHP or Java or Ruby or ...
   var ESTOffset = 5.0; 
</script>

Then you have to use a calculation like this:

 var hour = todaysDate.getUTCHours() + ESTOffset;

The hard part about that is is you display the message and expect it to change based on Eastern time. What if the user pops up the page and leaves it up until DST starts in EST? Do you need to handle that? What about if its almost 2 PM in EST and the page pops up? Do you have to pop up the message a few minutes later?

Case 3

You are in trouble if you need to figure out EST time, including DST, on a computer running an arbitrary time zone. There is nothing in javascript that lets you ask "What time is it in New York, USA?". You would need some library that knows the rules and that you will have to update (or make sure gets updated) if the rules change in EST. OR you could add some code yourself that knows when DST starts and ends in EST and does the math. Of course, then you have to update the code if the rules change.

Lee Meador
  • 12,829
  • 2
  • 36
  • 42
  • Lol no, to the last part. I don't have control over my server unfortunately, so I'm left with these JavaScript hacks that I'm not very good at. – henryaaron Oct 15 '13 at 18:32
0

This is easiest to do with a library. Using moment.js with moment-timezone:

$(document).ready(function(){

    var now = moment().tz("America/New_York");
    var weekday = now.day();
    var hour = now.hour();

    var isOpen = (weekday >= 0 && weekday <= 4 && hour >= 10 && hour < 20) || 
                 (weekday == 5 && hour >= 10 && hour < 16);

    if (isOpen) {
        $('div#announcements span').append('<br />Open.');
    }
});

Be sure to include the data for the America/New_York zone from the time zone data builder. This is the IANA time zone identifier for US Eastern Time.

The primary advantage here is that you can express your hours in terms of local time, which will take into account any variations of daylight saving time automatically. In other words, these times are in the US "Eastern Time" zone, rather than having to be specifically in either "Eastern Standard Time" or "Eastern Daylight Time".

Matt Johnson-Pint
  • 230,703
  • 74
  • 448
  • 575
  • Don't forget that things will need updating, at some indeterminate time in the future, if the rules for DST in the eastern time zone change. – Lee Meador Oct 15 '13 at 19:03
  • @LeeMeador - Good point. That's why there are multiple TZDB updates every year. Fortunately in the US they tend to announce these things in advance, but in some parts of the world they can change with short notice. – Matt Johnson-Pint Oct 15 '13 at 19:08