Code Snippet: Standard or Daylight Time

Share this page with your friends

A short time ago I was having a "discussion" with a friend about daylight saving time. She maintained that there was a federal law mandating the use of daylight time, and that we "springed ahead" and "falled back" at the same time all over the country. I argued that it was up to each state or county whether to adopt daylight time or not, and to decide when to change over.

It turns out we were both right, and we were both wrong. The law says that it is indeed up to each state or county whether to adopt daylight time, but that if they do, they must change over at the same time as everybody else.

When I discovered this, I immediately decided to write a script. I had always changed back and forth from CST to CDT by hand, which requires remembering to do it twice a year, and also remembering when it needs to be done. The owner of one of my websites was always getting email complaining that it was wrong.

So here is the script. I have seperated it into two functions. That way, if you just need to know in which segment of the year a given datetime is, you can use only one of the functions. And if you need to fill in the middle letter of the time zone designation, as I do, you can use both functions.

 1 function SorD(mydate)
 2 {  
 3   var newdate = new Date(mydate);
 4   var year = newdate.getFullYear();

 5   // Get the date to 'spring forward'
 6   var mar1 = new Date(year, 2, 1); // the date of Mar 1
 7   var sun2mar = 15 - mar1.getDay(); // the day of the 2nd Sunday in March
 8   var spring = new Date("Mar " + sun2mar + ", " + year + " 2:00:00");

 9   // Get the date to 'fall back'
10   var nov1 = new Date(year, 10, 1); // the date of Nov 1
11   var sun1nov = 8 - nov1.getDay(); // the day of the 1st Sunday in November
12   var fall = new Date("Nov " + sun1nov + ", " + year + " 2:00:00");

13   // Is it Daylight or Standard time?
14   if ((newdate > spring) && (newdate < fall))
15     { return "D"; }
16   else
17     { return "S"; }
18 }

 1 function fillSorD()
 2 {
 3   if (!document.getElementsByTagName("span")) return false;
 4   var today = new Date();
 5   var fillin = SorD(today);
 6   var spans = document.getElementsByTagName("span");
 7   for (var i=0; i<spans.length; i++)
 8     {
 9       if (spans[i].className == "SorD")
10         {
11           var txt = document.createTextNode(fillin);
12           spans[i].appendChild(txt);
13         }
14     }
15   return true;
16 }

How to use the script:

1) The first function, SorD, is the one that decides whether a given datetime is in the Standard or Daylight segment of the year. It can be used by itself to make such a decision.

2) The second function, fillSorD, shows one way to use the first function. The datetime of interest is NOW. It can be used to print the middle letter of the time zone designation automatically. For example, on one of my webpages is the statement, "The cut-off time for the weekly drawing is 5:00 PM CST Saturday." The middle letter of "CST" could be either an "S" or a "D", and this function makes that decision each time the page is loaded.

3) Consequently, the second function must be called when the webpage is loaded. Follow the instructions on the main Snippets Page for how to install a javascript function that runs at load time. The first function may be in the same off-page file, but does not need to run at load time.

4) Put the following code on your webpage wherever you want the middle letter to appear.  You must use a <span> tag, but the trade-off is that you can use the script more than one time per page if needed.

<span class="SorD"></span>

See it in action:   I won't bother to show you an example of the second function, since filling in a single letter is not very spectacular. However, I've set up a little demo of the first function, in which you can pick a datetime, and the SorD function will display either an "S" or a "D".

Enter any date (and optionally, the time) into the box below.

Then, when you move your mouse over the box below, an "S" or a "D" will appear showing whether it is Standard Time or Daylight Time at the location of your computer:

   < Close Box >

< Select Code > < Close Box >


Comments

There are currently no comments to display.

Click here to add a comment

Rate this page:






Share this page with your friends


Snippet Menu

  • Alert Box - Email
    Form Field Required
  • Table Rows with
    Alternating Colors
  • Track Hits from
    Your Backlinks
  • Protect your MailTo
    Link from Spammers
  • Overlay Box
    Instead of Popups
  • Form Submission
    No Double-Click
  • Change Dates
    Automatically
  • Is it Standard
    or Daylight Time?
  • Same Window
    or New Window?

RSS Feeds

  • Add 'Professors Coding Corner' to my feed reader.
  • Preview Professors Coding Corner on Feedage
  • Please preview this RSS feed on FeedAg, and rate it for me.
  • Professor's Ezine Articles about Web Programming
Watch a short video about how Forum2Blog Importer creates posts with comments for your Wordpress blog

Create a post for your blog
from any forum thread.
Use the first post as an article
and the replies as comments
Professor, 7807 Lerkenlund, St. Thomas, VI 00802 Tel: 340.715.3542
Copyright ©  Professional Website Design Website by: The Professor external link About Me Accessibility Legal Site Map
Professional Website Design logo

Professor's Coding Corner

  • Home
  • Code Snippets
  • Tutorials
  • Resources
    • HTML & CSS
    • Javascript
    • Perl
    • Tools
    • Other
  • Articles
  • Contact