Simple Daylight Savings Time
JavaScript |
I looked around for a script that would return whether is was daylight savings time or not. All I wanted to do was determine whether to write CST or CDT (Central Daylight Time) after printing the time on a page. Nothing fancy needed. All I found was a lot of long complicated or crappy scripts, some of which didn't work. So I wrote a couple. The first script is for USA time and aside from the obvious time zone anomalies associated with client-time JavaScript this little function will do the job. Close enuf fer guvment wurk! Oh and feel free to modify it for other times and places since the guvment seems to like to change the change every now and then. It works by calculating the 2nd Sunday in March and the 1st Sunday in November of the current year. Place the script in the head of your page and call it from anywhere. It returns true if DST is in effect. At the moment you loaded this page the script returned a value of . |
Below is another script that actually
determines if the user's computer is observing DST. It works by using
a time shifting trick. It determines the time zone shift relationship between January and July which indicates the user's
hemisphere. It then compares the current time zone offset to what the offset would be
when DST is not in effect.
If they don't match, it means the computer is currently observing DST.
One of the advantages of this script is that it is location independent
meaning that it will work in states or countries where DST is not observed. Example: (for northern hemisphere) If the client computer is set not to observe DST then the time zone offset will be the same as January even if the current date is in July, however if the current date reveals a time zone offset that is shifted from what it would be in January then DST is in effect. Place the script in the head of your page and call it from anywhere. It returns true if DST is in effect. At the moment you loaded this page the script returned a value of . |
Both of these scripts will determine DST but have slightly different uses. The first one is date based, the second reads the client computer setting. You can look at the source code of this page to see how they are used in html. Enjoy! |
|
If you find this code of value, donate a little to the cause. Think of the time you just saved! |
USE AT YOUR OWN RISK! |
Questions / Comments? Email me.
|
|