如何使用 JavaScript 了解一个国家/地区是否使用夏令时?

发布于 2024-10-02 23:21:57 字数 113 浏览 3 评论 0原文

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

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

但我怎么知道他的国家现在是否实行夏令时呢?有什么想法吗?

谢谢你!

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 his country is using DST now or not? Any ideas?

Thank you!

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

蛮可爱 2024-10-09 23:21:57

以下是使用 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');

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');
心凉 2024-10-09 23:21:57

检查这个脚本可能对你有帮助。参考:http://www.irt.org/< /strong>

<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>

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>
滴情不沾 2024-10-09 23:21:57

好吧,我只是拼凑了这段代码来确定当前选择的时区是否使用夏季调整以及当前是否有效。这是为 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.");
}

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.");
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文