开发者

How to know whether a country is using Daylight Saving Time using JavaScript?

I'm in China and I've a friend who is in other countries. e.g. UK, USA, New Zealand...

I know his country's name in English and time zone there.

But how can I know whether hi开发者_运维技巧s country is using DST now or not? Any ideas?

Thank you!


Here is an example using JSONP and some Yahoo services:

function jsonp(src){
        var s = document.createElement('script');
    s.charset = 'UTF-8';
    document.body.appendChild(s);
    s.src = src;
}
function getLocation(where){
    var src = 'http://query.yahooapis.com/v1/public/yql?callback=getTimeZone' + 
        '&q=select%20*%20from%20geo.places%20where%20text="'+ where +
        '"&format=json';
    jsonp(src);
}
function getTimeZone(response){
    var location = response.query.results.place[0].centroid, 
        src = 'http://query.yahooapis.com/v1/public/yql?callback=showTz' + 
        '&q=select%20*%20from%20xml%20where%20url%3D"http%3A%2F%2Fws.geonames.org%2Ftimezone%3Flat%3D' + 
        location.latitude +'%26lng%3D' +
        location.longitude + '"&format=json';
    jsonp(src);
}
function showTz(tz){
    console.log(tz.query.results.geonames.timezone);
}
getLocation('paris');


Check this script might help you. Reference: http://www.irt.org/

<SCRIPT LANGUAGE="JavaScript"><!--
function makeArray()    {
    this[0] = makeArray.arguments.length;
    for (i = 0; i<makeArray.arguments.length; i++)
        this[i+1] = makeArray.arguments[i];
}

var daysofmonth   = new makeArray( 31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31);
var daysofmonthLY = new makeArray( 31, 29, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31);

function LeapYear(year) {
    if ((year/4)   != Math.floor(year/4))   return false;
    if ((year/100) != Math.floor(year/100)) return true;
    if ((year/400) != Math.floor(year/400)) return false;
    return true;
}

function NthDay(nth,weekday,month,year) {
    if (nth > 0) return (nth-1)*7 + 1 + (7 + weekday -
DayOfWeek((nth-1)*7 + 1,month,year))%7;
    if (LeapYear(year)) var days = daysofmonthLY[month];
    else                var days = daysofmonth[month];
    return days - (DayOfWeek(days,month,year) - weekday + 7)%7;
}

function DayOfWeek(day,month,year) {
    var a = Math.floor((14 - month)/12);
    var y = year - a;
    var m = month + 12*a - 2;
    var d = (day + y + Math.floor(y/4) - Math.floor(y/100) +
Math.floor(y/400) + Math.floor((31*m)/12)) % 7;
    return d+1;
}

function y2k(number)    { return (number < 1000) ? number + 1900 : number; }

var today = new Date();
var year = y2k(today.getYear());

var DSTstart = new Date(year,4-1,NthDay(1,1,4,year),2,0,0);
var DSTend   = new Date(year,10-1,NthDay(-1,1,10,year),2,0,0);

function getMS(date) {
    return
Date.UTC(y2k(date.getYear()),date.getMonth(),date.getDate(),date.getHours(),date.getMinutes(),date.getSeconds())
}

var todayMS = getMS(today);
var DSTstartMS = getMS(DSTstart);
var DSTendMS = getMS(DSTend);

if (todayMS > DSTstartMS && todayMS < DSTendMS)
    document.write('Daylight Saving within effect');
else
    document.write('Daylght Saving NOT within effect');
//--></SCRIPT>


Well, I just cobbled together this bit of code to identify whether the currently selected time zone uses summer adjustment and whether it is currently in effect. This was written for Windows Scripting Host, but replace WScript.Echo with window.alert or whatever you prefer for whichever environment you choose to run it in...

// Solstice dates are approximate here!
// Don't give me grief about it varying from year to year.

var juneSolstice = new Date();
juneSolstice.setMonth(5); // zero-based
juneSolstice.setDate(21);

var decemberSolstice = new Date();
decemberSolstice.setMonth(11); // zero-based
decemberSolstice.setDate(21);

var now = new Date();

WScript.Echo(juneSolstice.getTimezoneOffset());
WScript.Echo(decemberSolstice.getTimezoneOffset());

var winterOffset = Math.max(juneSolstice.getTimezoneOffset(), decemberSolstice.getTimezoneOffset())
var summerOffset = Math.min(juneSolstice.getTimezoneOffset(), decemberSolstice.getTimezoneOffset())

if (juneSolstice.getTimezoneOffset() != decemberSolstice.getTimezoneOffset())
{
    WScript.Echo("This time zone uses summer time adjustment.");
}

if (now.getTimezoneOffset() != winterOffset)
{
    WScript.Echo("Summer time is currently in effect.");
}
else
{
    WScript.Echo("Summer time is currently NOT in effect.");
}
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜