javascript new Date() 设置为 2014 年 12 月 31 日改为 12 月 1 日
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/25741647/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
new Date() set to 31 december 2014 says 1st december instead
提问by user2859409
I am trying to convert a string to a Date object, and it works for all days except for December 31st where by object says December 1st instead of 31st. I have no idea why. Here is my JavaScript
code:
我正在尝试将字符串转换为 Date 对象,它适用于除 12 月 31 日之外的所有日子,其中对象表示 12 月 1 日而不是 31 日。我不知道为什么。这是我的JavaScript
代码:
var dt = new Date();
dt.setDate("31");
dt.setMonth("11");
dt.setFullYear("2014");
but my variable value is:
但我的变量值是:
Mon Dec 01 2014 11:48:08 GMT+0100 (Paris, Madrid)
If I do the same for any other date, my object returns to the appropriate value. Do you have any idea what I did wrong?
如果我对任何其他日期执行相同操作,我的对象将返回适当的值。你知道我做错了什么吗?
回答by Jakub Michálek
The thing is, when you set a day first, you're still in the current month, so September. September has only 30 days so:
问题是,当您首先设置日期时,您仍处于当前月份,即 9 月。九月只有 30 天,所以:
var dt = new Date(); /* today */
dt.setDate("31"); /* 1st Oct 2014 as should be by spec */
dt.setMonth("11"); /* 1st Dec 2014 */
dt.setFullYear("2014"); /* 1st Dec 2014 */
回答by xdazz
: (not safe for Months less than 31 days)setMonth
should before setDate
:(少于 31 天的月份不安全)setMonth
应该之前 setDate
var dt = new Date();
dt.setFullYear(2014);
dt.setMonth(11);
dt.setDate(31);
And setMonth
's second parameter also could be used to set date.
AndsetMonth
的第二个参数也可用于设置日期。
var dt = new Date();
dt.setFullYear(2014);
dt.setMonth(11, 31);
如果没有为构造函数提供参数,它将使用 the current date and time根据系统设置的当前日期和时间。
So, using setMonth
and setDate
separately would still cause unexpected result.
所以,单独使用setMonth
和setDate
仍然会导致意想不到的结果。
If the values set are greater than their logical range, the value will be auto adjusted to the adjacent value.
如果设置的值大于其逻辑范围,则该值将自动调整为相邻值。
For example, if today is 2014-09-30
, then
例如,如果今天是2014-09-30
,那么
var dt = new Date();
dt.setFullYear(2014); /* Sep 30 2014 */
dt.setMonth(1); /* Mar 02 2014, see, here the auto adjustment occurs! */
dt.setDate(28); /* Mar 28 2014 */
To avoid this, set the values using the constructor directly.
为避免这种情况,请直接使用构造函数设置值。
var dt = new Date(2014, 11, 31);
回答by funkybro
It's because the first thing you do is
这是因为你做的第一件事是
dt.setDate(31)
This sets the current date to 31. The current month is September which has 30 days, so it's wrapping it round.
这会将当前日期设置为 31。当前月份是 9 月,有 30 天,因此它正在将其环绕。
If you were to print out the date after this point, it would say 1 October.
如果您要在此之后打印日期,则会显示 10 月 1 日。
回答by Salman A
Assuming your intent is to set year, month and date simultaneously you could use the longer date constructor:
假设您的意图是同时设置年、月和日期,您可以使用更长的日期构造函数:
new Date(year, month, day, hour, minute, second, millisecond);
[...]
If at least two arguments are supplied, missing arguments are either set to 1 (if day is missing) or 0 for all others.
新日期(年、月、日、时、分、秒、毫秒);
[...]
如果提供了至少两个参数,缺少的参数要么设置为 1(如果缺少日期),要么为所有其他参数设置为 0。
So you would write:
所以你会写:
var dt = new Date(2014, 11, 31);
As already established, setting one portion of date at a time can result in overflows:
如前所述,一次设置一部分日期可能会导致溢出:
var dt = new Date(2012, 1, 29); // Feb 29 2012
dt.setFullYear(2014); // Mar 01 2014 instead of Feb 28 2014
Moreover, setting month before date can still cause unexpected overflow (answers that recommend changing the order of methods are incorrect):
此外,在日期之前设置月份仍然会导致意外溢出(建议更改方法顺序的答案不正确):
var dt = new Date(2014, 0, 31); // Jan 31 2014
dt.setFullYear(2014); // Jan 31 2014
dt.setMonth(1); // Mar 03 2014 instead of Feb 28 2014
dt.setDate(1); // Mar 01 2014
回答by Florian F
The why of the behaviour and how to avoid it has been amply explained.
已经充分解释了行为的原因以及如何避免它。
But the real error in your code is that you shouldn't use the default constructor: new Date(). Your code will result in a Date on Dec. 13 with the current time. I doubt this is what you want. You should use the Date constructor that takes year, month and day as parameters.
但是你的代码中真正的错误是你不应该使用默认的构造函数:new Date()。您的代码将生成一个日期为 12 月 13 日的当前时间。我怀疑这就是你想要的。您应该使用将年、月和日作为参数的 Date 构造函数。
回答by henman
The answers made clear that the right order for setting the date is:
答案清楚地表明,设置日期的正确顺序是:
- setFullYear()
- setMonth()
- setDate()
- setFullYear()
- 设置月份()
- 设置日期()
I just want to point out that it′s also important to set the year at first, because of the 29. february in leapyears.
我只想指出,首先设置年份也很重要,因为闰年是 2 月 29 日。
回答by Deepak Sharma
var dt = new Date();
dt.setFullYear(2014);
dt.setMonth(11);
dt.setDate(31);
Pass value as integer not string.. it will return u correct value..
将值作为整数而不是字符串传递..它会返回你正确的值..
Update- above description is not correct.. the main issue was you need to put these three line in proper sequence.. Even After I corrected the sequence I forgot to correct the description.. :P
更新- 上面的描述不正确..主要问题是你需要把这三行按正确的顺序排列..即使在我纠正了序列之后我也忘记纠正描述..:P