我在中国,我有一个朋友在其他国家。例如英国、美国、新西兰……
我知道他的国家的英文名称和时区。
但是我怎么知道他的国家现在是否使用 DST 呢?有任何想法吗?
谢谢!
我在中国,我有一个朋友在其他国家。例如英国、美国、新西兰……
我知道他的国家的英文名称和时区。
但是我怎么知道他的国家现在是否使用 DST 呢?有任何想法吗?
谢谢!
下面是一个使用 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');
检查此脚本可能会对您有所帮助。参考: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>
好吧,我只是拼凑了这段代码,以确定当前选择的时区是否使用夏季调整,以及当前是否生效。这是为 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.");
}