Javascript Date()构造函数不起作用

编程入门 行业动态 更新时间:2024-10-27 09:45:38
本文介绍了Javascript Date()构造函数不起作用的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述

我有一个问题 -

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()构造函数不起作用

本文发布于:2023-11-22 19:04:24,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1618635.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:函数   不起作用   Javascript   Date

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!