Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Offset time for DST in one specific timezone using JavaScript

I need to offset the time by an hour if it's currently DST in the Pacific Time Zone. How can I determine the current daylight savings status of the Pacific Time Zone, regardless of the user's local timezone?

Here's what I have so far. "dst" in line 4 is just a placeholder for a function that would tell me if daylight savings time is active in that zone.

function checkTime() {  
    var d = new Date();  
    var hour = d.getUTCHours();  
    var offset = dst ? 7 : 8; // is pacific time currently in daylight savings?

    // is it currently 6 AM, 2 PM, or 10 PM?
    if (hour === ((6 + offset) % 24) || hour === ((14 + offset) % 24) || hour === ((22 + offset) % 24)) {  
      // do stuff
    }
}

checkTime();
like image 312
Shannon Avatar asked Jul 07 '10 01:07

Shannon


1 Answers

The only real way you will be able to tell for sure is if you have the DST information (including any updates) available on a server you can ask. The user's PC does not need to even have the timezone information installed.

Then you need a "dynamic" script to get the DST status.

Possible methods: Reference a PHP/ASPX/JSP/CFM/ASP page that generates a javascript to set a variable that indicates if DST is active or not, or the current number of minutes offset.

Write the script in your (hopefully dynamic) page directly.

Use AJAX/REST/JSON to access a Date/Time service somewhere that can tell you the DST status or number of minutes offset from UTC for a timezone/location.

Minutes is preferable to hours because DST offsets are actually in minutes around the world, not hours.

like image 179
Roger Willcocks Avatar answered Sep 20 '22 12:09

Roger Willcocks