Javascript Date()构造函数不起作用 [英] Javascript Date() constructor doesn't work
问题描述
我有一个问题 -
javascript 日期(mm-dd-yyyy)
构造函数没有为FF工作。它适用于IE。
The javascript Date("mm-dd-yyyy")
constructor doesn't work for FF. It works fine for IE.
- IE:
新日期(04-02-2008)
=>Wed Apr 2 00:00:00 EDT 2008
- FF2:
新日期(04-02-2008)
=>无效日期
- IE :
new Date("04-02-2008")
=>"Wed Apr 2 00:00:00 EDT 2008"
- FF2 :
new Date("04-02-2008")
=>Invalid Date
所以让我们试试另一个构造函数。试试这个构造函数日期(yyyy,mm,dd)
So lets try another constructor. Trying this constructor Date("yyyy", "mm", "dd")
- IE:
新日期(2008,04,02);
=>Fri May 2 00:00:00 EDT 2008
- FF:
新日期(2008,04,02);
=>Fri May 2 00 00:00:00 EDT 2008
- IE:
new Date (2008,03,02);
=>Wed Apr 2 00:00:00 EDT 2008
- FF:
新日期(2008,03,02);
=>周三2008年4月2日00:00:00
- IE :
new Date("2008", "04", "02");
=>"Fri May 2 00:00:00 EDT 2008"
- FF :
new Date("2008", "04", "02");
=>"Fri May 2 00:00:00 EDT 2008"
- IE :
new Date("2008", "03", "02");
=>"Wed Apr 2 00:00:00 EDT 2008"
- FF :
new Date("2008", "03", "02");
=>"Wed Apr 2 00:00:00 EDT 2008"
所以日期( yyyy,mm,dd)
构造函数使用索引 0
来表示1月。
So the Date("yyyy", "mm", "dd")
constructor uses an index of 0
to represent January.
有没有人处理过这个问题?
必须有一个比从月中减去1更好的方法。
Has anyone dealt with this?
There must be a better way than subtracting 1 from the months.
推荐答案
这是定义Date对象使用值0-11表示月
字段。
It is the definition of the Date object to use values 0-11 for the month
field.
我相信使用String的构造函数是依赖于系统的(更不用说依赖于语言环境/时区),因此您最好使用构造函数,将年/月/日指定为单独的参数。
I believe that the constructor using a String is system-dependent (not to mention locale/timezone dependent) so you are probably better off using the constructor where you specify year/month/day as seperate parameters.
BTW,在Firefox中,
BTW, in Firefox,
new Date("04/02/2008");
对我来说很好 - 它会解释斜线,但不能解释连字符。我认为这证明了我的观点,即使用String构造Date对象是个问题。改为使用月/日/年的显式值:
works fine for me - it will interpret slashes, but not hyphens. I think this proves my point that using a String to construct a Date object is problemsome. Use explicit values for month/day/year instead:
new Date(2008, 3, 2);
这篇关于Javascript Date()构造函数不起作用的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!