如何知道某个国家/地区是否正在使用JavaScript进行夏令时?

时间:2010-11-19 08:01:37

标签: javascript dst

我在中国,我是一个在其他国家的朋友。例如英国,美国,新西兰......

我知道他的国家的英文名称和时区。

但我怎么知道他的国家现在是否正在使用DST?有什么想法吗?

谢谢!

3 个答案:

答案 0 :(得分:2)

以下是使用JSONP和一些Yahoo服务的示例:

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

答案 1 :(得分:0)

检查此脚本可能对您有所帮助。参考: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>

答案 2 :(得分:0)

好吧,我只拼凑了这段代码,以确定当前选定的时区是否使用夏季调整以及当前是否有效。这是为Windows Scripting Host编写的,但是将WScript.Echo替换为window.alert或您喜欢的任何环境,无论您选择在哪个环境中运行它...

// 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.");
}