JS日期 - JS引擎错误,或者我正在失去理智?! [英] JS Date - JS Engine bug, or I'm losing my mind?!

查看:54
本文介绍了JS日期 - JS引擎错误,或者我正在失去理智?!的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

祝福所有人。我的第一篇文章 - 希望有人可以提供帮助。


首先我在FF 1.5.0.9,2.x上测试了这个,并在OSX上测试了它。


这里是展示行为的代码:


#case 1 - 失败

var date1 = new Date();

date1.setYear(2007);

date1.setMonth(3);

date1.setDate(10);

alert(''(2007-3-10)::''+ date1.getFullYear()+'' - ''+(date1.getMonth())+'' - ''+ date1.getDate() ); // 2007-4-10

alert(''(2007年4月10日)::''+ date1.toDateString()); #Thu 2007年5月10日


#case 2 - 成功 - 预期行为

var date2 = new Date();

date2 .setYear(2007);

date2.setMonth(4);

date2.setDate(12);

alert(''(2007) -4-12)::''+ date2.getFullYear()+'' - ''+(date2.getMonth())+'' - ''+ date2.getDate()); // 2007-4-12

alert(''(2007年5月12日)::''+ date2.toDateString()); // #Sat May 2007 2007


所以我看到一个讨厌的行为:

date1.setMonth + 1 == date1.getMonth


我在第3个月(4月)才发现这种情况。


我是否会失去理智(也许是错误的群体)?或者有人可以帮助我确定我的逻辑中缺少什么 - 因为如果浏览器中的错误是相同的(除非在osx中​​FF和Safari都是相同的JS引擎),这是不寻常的。


我接下来会尝试IE浏览器。


TIA!

Jodi

Greetings all. My 1st post here - hoping someone can help out.

First I''ve tested this on FF 1.5.0.9, 2.x and Safari on OSX.

here''s the code that exhibits the behaviour:

#case 1 - fails
var date1 = new Date();
date1.setYear(2007);
date1.setMonth(3);
date1.setDate(10);
alert(''(2007-3-10) ::'' + date1.getFullYear() + ''-'' + (date1.getMonth()) + ''-'' + date1.getDate()); // 2007-4-10
alert(''(April 10, 2007) ::'' + date1.toDateString()); #Thu May 10 2007

#case 2 - succeeds - expected behaviour
var date2 = new Date();
date2.setYear(2007);
date2.setMonth(4);
date2.setDate(12);
alert(''(2007-4-12) ::'' + date2.getFullYear() + ''-'' + (date2.getMonth()) + ''-'' + date2.getDate()); // 2007-4-12
alert(''(May 12, 2007) :: '' + date2.toDateString()); // #Sat May 12 2007

So I''m seeing a nasty behaviour:
date1.setMonth + 1 == date1.getMonth

I''ve only seen this happen for month 3 (April).

Am I losing my mind (perhaps the wrong group)? Or can someone help me identify what''s missing in my logic - because it would be unusual if a bug was identical across browsers (unless the JS engine is the same in both FF and Safari in osx).

I''ll try IE Windows next.

TIA!
Jodi

推荐答案


问候所有。我的第一篇文章 - 希望有人可以提供帮助。


首先我在FF 1.5.0.9,2.x上测试了这个,并在OSX上测试了它。


这里是展示行为的代码:


#case 1 - 失败

var date1 = new Date();

date1.setYear(2007);

date1.setMonth(3);

date1.setDate(10);

alert(''(2007-3-10)::''+ date1.getFullYear()+'' - ''+(date1.getMonth())+'' - ''+ date1.getDate() ); // 2007-4-10

alert(''(2007年4月10日)::''+ date1.toDateString()); #Thu 2007年5月10日


#case 2 - 成功 - 预期行为

var date2 = new Date();

date2 .setYear(2007);

date2.setMonth(4);

date2.setDate(12);

alert(''(2007) -4-12)::''+ date2.getFullYear()+'' - ''+(date2.getMonth())+'' - ''+ date2.getDate()); // 2007-4-12

alert(''(2007年5月12日)::''+ date2.toDateString()); // #Sat May 2007 2007


所以我看到一个讨厌的行为:

date1.setMonth + 1 == date1.getMonth


我在第3个月(4月)才发现这种情况。


我是否会失去理智(也许是错误的群体)?或者有人可以帮助我确定我的逻辑中缺少什么 - 因为如果浏览器中的错误是相同的(除非在osx中​​FF和Safari都是相同的JS引擎),这是不寻常的。


我接下来会尝试IE浏览器。


TIA!

Jodi
Greetings all. My 1st post here - hoping someone can help out.

First I''ve tested this on FF 1.5.0.9, 2.x and Safari on OSX.

here''s the code that exhibits the behaviour:

#case 1 - fails
var date1 = new Date();
date1.setYear(2007);
date1.setMonth(3);
date1.setDate(10);
alert(''(2007-3-10) ::'' + date1.getFullYear() + ''-'' + (date1.getMonth()) + ''-'' + date1.getDate()); // 2007-4-10
alert(''(April 10, 2007) ::'' + date1.toDateString()); #Thu May 10 2007

#case 2 - succeeds - expected behaviour
var date2 = new Date();
date2.setYear(2007);
date2.setMonth(4);
date2.setDate(12);
alert(''(2007-4-12) ::'' + date2.getFullYear() + ''-'' + (date2.getMonth()) + ''-'' + date2.getDate()); // 2007-4-12
alert(''(May 12, 2007) :: '' + date2.toDateString()); // #Sat May 12 2007

So I''m seeing a nasty behaviour:
date1.setMonth + 1 == date1.getMonth

I''ve only seen this happen for month 3 (April).

Am I losing my mind (perhaps the wrong group)? Or can someone help me identify what''s missing in my logic - because it would be unusual if a bug was identical across browsers (unless the JS engine is the same in both FF and Safari in osx).

I''ll try IE Windows next.

TIA!
Jodi



我只是骗了这个......

我在内心哭泣。


您可以使用setFullDate并且它可以正常工作。

I just fooled around with this...
I''m crying on the inside.

You can use setFullDate and it works correctly.


Thanx你的回复。


我会尝试setFullDate - 但很有趣,我找不到一个谷歌参考这个方法!


你能告诉我你测试的浏览器是什么吗?


Thanx。

Jodi
Thanx your response.

I''ll try out setFullDate - but funny, I can''t find a single google reference to this method!

Can you please tell me what browser you tested on?

Thanx.
Jodi



谢谢你的回复。


我会尝试setFullDate - 但很有趣,我找不到这个方法的单一谷歌参考!


你能告诉我是什么b你测试过的rowser?


Thanx。

Jodi
Thanx your response.

I''ll try out setFullDate - but funny, I can''t find a single google reference to this method!

Can you please tell me what browser you tested on?

Thanx.
Jodi



完全对不起!

我的意思是setFullYear函数!

Entirely sorry!
What I meant to say was the setFullYear function!


这篇关于JS日期 - JS引擎错误,或者我正在失去理智?!的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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