时区 [英] time zone

查看:114
本文介绍了时区的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述



我要求比较用户输入为mm / dd / yy / HH / mm

(月,日,年,小时,分钟)到复活节标准时间。

我使用下面的脚本,但它没有给出正确的答案。

非常感谢您的帮助。谢谢!

//这是用户输入的内容

var gdate = new Date(f.year.value,f.month.value,f.day.value,

f.hour.value,f.min.value);

var EST_offset = -4

var num_msecs = gdate.getTime() + gdate.getTimezoneOffset()* 60000

+ EST_offset * 3600000;


//桌面当前时间取决于位置

var gdate2 = new Date();

var num_msecs2 = gdate2.getTime()+ gdate2.getTimezoneOffset()*

60000;

//我们不知道GMT的偏差!什么getTimezoneOffset

返回?


if(num_msecs< num_msecs2){

alert(" Schedule time)必须是在将来);

返回false;

}

Hi
I have a requirement to compare the user entry for mm/dd/yy/HH/mm
(month, day, year, hour, minute) to the Easter Standard Time.
I am using the following script, but it is not giving the right answer.
Your kind help is appreciated. Thank you!
//this is what user enters
var gdate = new Date(f.year.value, f.month.value, f.day.value,
f.hour.value, f.min.value);
var EST_offset = -4
var num_msecs = gdate.getTime()+ gdate.getTimezoneOffset() * 60000
+ EST_offset *3600000;

//the current time of the desk top depends on location
var gdate2 = new Date();
var num_msecs2 = gdate2.getTime()+ gdate2.getTimezoneOffset() *
60000;
//we don''t know the offset from GMT! What does getTimezoneOffset
return?

if (num_msecs < num_msecs2) {
alert("Schedule time has to be in the future");
return false;
}

推荐答案

so***********@yahoo.com 写道:

[snip]
so***********@yahoo.com wrote:
[snip]
//我们不知道GMT的偏差! getTimezoneOffset
返回什么?
//we don''t know the offset from GMT! What does getTimezoneOffset
return?




客户从Zulu抵消(减去)的本地分钟数

时间。


格林威治以西,东方负面。

Mick



Local number of minutes that the client is offset(subtracted) from Zulu
Time.

West of Greenwich positive, East negative.
Mick


谢谢Mick。什么是祖鲁时间?这就是我最终编码的方式,

记录


//表格日期/时间验证


//在美国,dst从4月的第一个星期日开始,到最后的

结束.... 10月的星期日....


//四月的第一个星期日的日期是:


var curYear = new Date(); //获取年份(或手动设置)

//设置所选年份的4月1日

//记住月份为零,所以4月= 3

var startDST = new Date(curYear.getFullYear(),3,1); //设置一个

日期到4月1日

//使用一周的日期数作为抵消

//减去7(周内#天)

//并加到第1

startDST.setDate(1 + 7 - startDST.getDay());

//设置10月的最后一天

var endDST = new Date(curYear.getFullYear(),9,31);

//减去日期中的数字最后一天的一周到达最后一个星期

星期日的星期日

endDST.setDate(31 - endDST.getDay());

var gdate =新日期(f.year.value,f.month.value,f.day.value,

f.hour.value,f.min.value);

var tzoffset = -4;

if(gdate< startDST || gdate> endDST)

tzoffset = -5;


var num_msecs = gdate.getTime();

var gdate2 = new Date();

var num_msecs2 = gdate2.getTime()+ gdate.getTimezoneOffset() *

60000 + tzoffset * 3600000;

// alert(" diff =" +(num_msecs - num_msecs2)/(60000 * 60)+ "偏移= QUOT; +

gdate.getTimezoneOffset()+" tzoffset =" + tzoffset);

if(num_msecs< num_msecs2){

alert(预定时间必须在将来);

返回false;

}

Thanks Mick. What is zulu time ? This is how I ended up coding, for the
record

//Form date/time validation

//In the US, dst starts on the first sunday in april and ends on
the last
//sunday in october..

//the date of the 1st sunday in april is:

var curYear = new Date(); // get the year (or manually set it)
// set up the 1st of april of the chosen year
// remember months are zero based, so april = 3
var startDST = new Date(curYear.getFullYear(), 3, 1); // sets a
date to 1st of april
// use the day number of the week as an offset
// by subtracting from 7 (# days in week)
// and add to 1st
startDST.setDate(1 + 7 - startDST.getDay());
// set up last day of october
var endDST = new Date(curYear.getFullYear(), 9, 31);
// subtract the day number in week of last day to arrive at last
sunday in month
endDST.setDate(31 - endDST.getDay());
var gdate = new Date(f.year.value, f.month.value, f.day.value,
f.hour.value, f.min.value);
var tzoffset = -4;
if ( gdate < startDST || gdate > endDST )
tzoffset = -5;

var num_msecs = gdate.getTime();
var gdate2 = new Date();
var num_msecs2 = gdate2.getTime() + gdate.getTimezoneOffset() *
60000 + tzoffset*3600000;
//alert("diff=" + (num_msecs - num_msecs2)/(60000*60) + "offset=" +
gdate.getTimezoneOffset() + "tzoffset=" + tzoffset );
if (num_msecs < num_msecs2) {
alert("Schedule time has to be in the future");
return false;
}


所以*********** @ yahoo.com 写道:
谢谢米克。什么是祖鲁时间?这就是我最终编码的方式,因为
记录


你的算法看起来有缺陷。


第一个问题是检查用户输入的值是

有效数字,并生成一个有效的日期和时间 - 希望你这样做

在其他地方,只是没有显示它。


第二个问题是你依赖客户的本地系统

设置日期,时间,地区和夏令时是否正确

他们可能不是很好的比例。


第三个问题是夏令时更改是在凌晨02:00进行的,

你的算法假设变化发生在00:00 AM。


最后,你找到4月第一个星期日的算法失败了

它下降了在4月1日(2012年)。


我认为你最好总是在服务器上做这些东西。


//表格日期/时间验证
//在美国,dst从四月的第一个星期日开始,到十月的最后一个星期天结束。

//日期四月的第一个星期日是:

var curYear = new Date(); //获取年份(或手动设置)
//设置所选年份的4月1日
//记住月份为零,所以4月= 3
var startDST =新日期(curYear.getFullYear(),3,1); //设置


var startDST =新日期(curYear.getFullYear(),3,1,2);

日期到4月1日
//使用星期的天数作为偏移量
//从7减去(一周中的#天数)
//并添加到1st
startDST.setDate(1 + 7 - startDST.getDay());


var startDST = new Date(curYear.getFullYear(),3,1,2);

startDST。 setDate(1 +(7-d.getDay())%7);

//设置10月的最后一天
var endDST = new Date(curYear.getFullYear(),9 ,31);
Thanks Mick. What is zulu time ? This is how I ended up coding, for the
record
Your algorithm appears flawed.

The first issue is checking that the values entered by the user are
valid numbers and generate a valid date and time - hopefully you do that
elsewhere and just didn''t show it.

The second issue is that you are dependent on the client''s local system
settings for date, time, region and daylight saving being correct which
they likely aren''t in a good percentage of cases.

The third issue is that daylight saving changes are made at 02:00am,
your algorithm assumes that the change happens at 00:00 AM.

Lastly, your algorithm for finding the first Sunday in April fails when
it falls on the 1st of April (2012).

I think you are better off to always do this stuff on the server.


//Form date/time validation

//In the US, dst starts on the first sunday in april and ends on
the last
//sunday in october..

//the date of the 1st sunday in april is:

var curYear = new Date(); // get the year (or manually set it)
// set up the 1st of april of the chosen year
// remember months are zero based, so april = 3
var startDST = new Date(curYear.getFullYear(), 3, 1); // sets a
var startDST = new Date(curYear.getFullYear(), 3, 1, 2);
date to 1st of april
// use the day number of the week as an offset
// by subtracting from 7 (# days in week)
// and add to 1st
startDST.setDate(1 + 7 - startDST.getDay());

var startDST = new Date(curYear.getFullYear(), 3, 1, 2 );
startDST.setDate( 1 + (7-d.getDay())%7 );

// set up last day of october
var endDST = new Date(curYear.getFullYear(), 9, 31);




var endDST = new Date(curYear.getFullYear(),9,31,2);

[.. 。]


-

Rob



var endDST = new Date(curYear.getFullYear(), 9, 31, 2);
[...]

--
Rob


这篇关于时区的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆