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();

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.


I've also struggled with this before in a PHP based website which is supposed to display the "world clock". There's no nice way to achieve this in JS and even then, you're heavily dependent on the client's environment. You really need to do this task in the server side language you're using.

If it happens that you're using PHP as well, then you may find this snippet useful:

function get_offset_with_DST($timezone_name) {
    $default_timezone = date_default_timezone_get();
    date_default_timezone_set($timezone_name);
    $offset_with_DST = date('Z') / 3600;
    date_default_timezone_set($default_timezone);
    return $offset_with_DST;
}

and then in JS:

<script>var offsetWithDST = <?= get_offset_with_DST('US/Pacific'); ?>;</script>

For a list of all supported timezone names in PHP, check this page.


In the USA, DST is currently calculated as the time falling between the 2nd Sunday in March and the 1st Sunday in November.

To calculate the date DST begins, we first calculate the current year, then what weekday the first of March fall on, and based on that we can calculate the 2nd Sunday. The same is done for November, and finally a test of whether today's date falls between the two anchor dates. dst is turned to "1" when true.

For my needs day-level precision is adequate, but it could be built out to greater precision.

    var dst = 0
    var d = new Date();
    var myYear = parseInt(d.getFullYear())
    d.setFullYear(myYear , 2, 1);
    var what_Weekday_Mar1 = d.getDay();

    var DST_StartDay

    switch(what_Weekday_Mar1) {
        case 0:
            DST_StartDay = 14
            break;
        case 1:
            DST_StartDay = 13
            break;
        case 2:
            DST_StartDay = 12
            break;
        case 3:
            DST_StartDay = 11
            break;
        case 4:
            DST_StartDay = 10
            break;
        case 5:
            DST_StartDay = 9
            break;
        case 6:
            DST_StartDay = 8
            break;
     }

    var DST_on_Date = new Date()
    DST_on_Date = DST_on_Date.setFullYear(myYear,2,DST_StartDay)

    d.setFullYear(myYear , 10, 1);
    var what_Weekday_Nov1 = d.getDay();

    switch(what_Weekday_Nov1) {
        case 0:
            DST_EndDay = 1
            break;
        case 1:
            DST_EndDay = 7
            break;
        case 2:
            DST_EndDay = 6
            break;
        case 3:
            DST_EndDay = 5
            break;
        case 4:
            DST_EndDay = 4
            break;
        case 5:
            DST_EndDay = 3
            break;
        case 6:
            DST_EndDay = 2
            break;
     }
    var DST_off_Date = new Date()
    DST_off_Date = DST_off_Date.setFullYear(myYear,10,DST_EndDay)

    var toDay = new Date();

    /*if today is between Daylight Savings On and Off times */
    if (today>DST_on_Date && today<DST_off_Date) {
        dst = 1;    
    }

hope this helps!

-RL

链接地址: http://www.djcxy.com/p/1648.html

上一篇: 如何在没有手机应用的情况下构建Android?

下一篇: 使用JavaScript在特定时区为DST设置偏移时间