Javascript Date.getMonth() 方法有错误?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/14680396/
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
the Date.getMonth() method has bug?
提问by u1181816
I tested in chrome and firefox, and here is the problem.
The Date object's getMonth()method has an Bug?
When I set the location date on someDay, like "2013-1-31"
我在 chrome 和 firefox 中进行了测试,这是问题所在。Date 对象的getMonth()方法有一个 Bug?当我在某天设置位置日期时,例如"2013-1-31"
then, the js code is :
然后,js代码是:
var d = new Date();
d.setMonth(8);
d.getMonth();
The result is "9", why?
结果是“9”,为什么?
I found when the month has 31 days, run the setMonthmethod and the getMonthmethod will return wrong value
我发现当月有 31 天时,运行该setMonth方法,该getMonth方法将返回错误值
why?
为什么?
回答by wcraft
The getMonth() method returns the month (from 0 to 11) for the specified date, according to local time.
getMonth() 方法根据本地时间返回指定日期的月份(从 0 到 11)。
Note: January is 0, February is 1, and so on.
注:一月为 0,二月为 1,依此类推。
回答by hrunting
Let's break this down:
让我们分解一下:
var d = new Date(); // date is now 2013-01-31
d.setMonth(1); // date is now 2013-02-31, which is 3 days past 2013-02-28
x = d.getMonth(); // what to do, what to do, 3 days past 2013-02-28 is in March
// so, expect x to be March, which is 2
This is only an issue when the day value of dis greater than the maximum number of days in the month passed to setMonth(). Otherwise, it works as you'd expect.
只有当 的天值d大于传递给 的月份中的最大天数时,才会出现此问题setMonth()。否则,它会按您的预期工作。
回答by reetah
Simplest solution to this is to add second argument to setMonth:
最简单的解决方案是向 setMonth 添加第二个参数:
var d = new Date();
d.setMonth(8,1);
d.getMonth(); //outputs 8
http://www.w3schools.com/jsref/jsref_setmonth.asp
http://www.w3schools.com/jsref/jsref_setmonth.asp
Date.setMonth(month,day)
day: Optional. An integer representing the day of month Expected values are 1-31, but other values are allowed:
0 will result in the last day of the previous month -1 will result in the day before the last day of the previous month If the month has 31 days:
32 will result in the first day of the next month If the month has 30 days:
32 will result in the second day of the next month
Date.setMonth(月,日)
天:可选。表示月份中某天的整数 预期值为 1-31,但允许其他值:
0 将导致上个月的最后一天 -1 将导致上个月的最后一天的前一天 如果该月有 31 天:
32 将导致下个月的第一天 如果该月有 30 天:
32 将导致下个月的第二天
回答by syazdani
Months in JavaScript are represented from 0-11. Month 1 would be February which only has 28/29 days, so when you set the month to 1, it tries to auto-correct the date to March to make a date that makes sense (since Feb 31st, makes no sense). Try it out by using the toDateString function to see what I mean:
JavaScript 中的月份从 0 到 11 表示。第 1 个月将是只有 28/29 天的二月,因此当您将月份设置为 1 时,它会尝试将日期自动更正为三月以创建一个有意义的日期(自 2 月 31 日以来,没有意义)。使用 toDateString 函数试试看,看看我的意思:
var d = new Date('2013/01/31');
d.setMonth(2);
console.log(d.toDateString()); // outputs Match 3rd, 2013
A little weird perhaps, but not buggy.
也许有点奇怪,但不是马车。
回答by sohel khalifa
The getMonth()method returns the month (from 0 to 11) for the specified date, according to local time, eg. January is 0, February is 1.
该getMonth()方法根据本地时间返回指定日期的月份(从 0 到 11),例如。一月为 0,二月为 1。
回答by Iswanto San
In javascript month is start from 0. Assume today is 02/04/2012, when you setMonth(1) it will try to set to feb. Since max day in feb is 28/29, it move to the next month (March, which is 2)
在javascript中,月份从0开始。假设今天是02/04/2012,当您setMonth(1)时,它会尝试设置为feb。由于 2 月的最大天数是 28/29,它移到下个月(3 月,即 2)
回答by fresko
The setMonth here is not what you expect, and what happens in better libraries. If you set a date 31.x.yyyy to x+1, probably the next month (x+1) will not have 31 days. So 31.x+1.yyyy will be changed to 1.x+2.yyyy.
这里的 setMonth 不是你所期望的,在更好的库中会发生什么。如果将日期 31.x.yyyy 设置为 x+1,则下个月 (x+1) 可能没有 31 天。所以31.x+1.yyyy会变成1.x+2.yyyy。
In such case, the month will be increased by 2 - the date completely changed.
在这种情况下,月份将增加 2 - 日期完全改变。
I had this problem with changing February->March, thanks God that was only a test.
我在更改二月->三月时遇到了这个问题,感谢上帝,这只是一个测试。
回答by JeffG
This has to do with the current local date, even if the date you're setting is in the past or future(assuming you are correctly using the month range 0-11). This is because when you initialize Date(), it is using the current date. When you modify the date by using setFullYear(), setMonth() or setDate(), the default "current" values are still present and can shift the date.
这与当前本地日期有关,即使您设置的日期是过去或将来(假设您正确使用了 0-11 月份范围)。这是因为当您初始化 Date() 时,它使用的是当前日期。当您使用 setFullYear()、setMonth() 或 setDate() 修改日期时,默认的“当前”值仍然存在并且可以改变日期。
If your current day of the month (today) is the 31st of the month and you then set a date in the past using setMonth(), setDate() where the month has less than your current 31 days, then Date() will give you the wrong month, even if you specifically state it. This is because when you setMonth(), the current date value of 31, which doesn't exist in the month you set, pushes it into the next month. Then when you set the date, the month never changes back.
如果您当月的当前日期(今天)是本月的 31 日,然后您使用 setMonth()、setDate() 设置过去的日期,其中当月少于您当前的 31 天,则 Date() 将给出你错了月份,即使你特别说明。这是因为当您 setMonth() 时,当前日期值 31(在您设置的月份中不存在)会将其推入下个月。然后当您设置日期时,月份永远不会变回。
If you set the day of the month before the month - setDate(), setMonth(), this fixes the problem, but has another similar issue / bug. If your current month only has 30 days and the date you're trying to set has 31 days, it will set the date of the month as 1, not 31. This is resolved if you set the month before the day - setMonth(), setDate(), but then you're back to the first problem.
如果您在月份之前设置月份中的某天 - setDate()、setMonth(),这可以解决问题,但还有另一个类似的问题/错误。如果您当前的月份只有 30 天,而您尝试设置的日期有 31 天,它会将月份的日期设置为 1,而不是 31。如果您将月份设置在前一天 - setMonth() , setDate(),但是你又回到了第一个问题。
Solutions:
解决方案:
- Get the values and then set the day of the month and month at the same time (as reetah suggested) - setMonth(8,1)
- Set the year with optional values to initialize setFullYear(1972, 0, 1), then setMonth, setDate.
- Set the month, set the day, then verify the month equals what you set it to and set it again if not - setMonth(), setDate(), setMonth()
- 获取值,然后同时设置月份和月份的日期(如 reetah 建议的那样)- setMonth(8,1)
- 使用可选值设置年份以初始化 setFullYear(1972, 0, 1),然后是 setMonth, setDate。
- 设置月份,设置日期,然后验证月份是否等于您设置的月份,如果不是,则再次设置 - setMonth(), setDate(), setMonth()
function formatDate(date) {
var monthNames = [
"January", "February", "March",
"April", "May", "June", "July",
"August", "September", "October",
"November", "December"
];
var day = date.getDate();
var monthIndex = date.getMonth();
var year = date.getFullYear();
return day + ' ' + monthNames[monthIndex] + ' ' + year;
}
var date = new Date();
if (date.getDate() < 31) {
$("#mydate").text("YOUR LOCAL DATE (on your computer) MUST BE SET TO THE 31st for this bug to show.");
} else {
date.setFullYear(1972);
date.setMonth(8); // (month-1)
date.setDate(24);
var breakdown = "<br>Day: " + date.getDate() + "<br>Month: " + date.getMonth() + "<br>Year: " + date.getFullYear();
$("#mydate").html("Set as September (8)<br><br>" + formatDate(date) + breakdown);
var date = new Date();
date.setFullYear(1972);
date.setMonth(8, 24); // (month-1)
var breakdown = "<br>Day: " + date.getDate() + "<br>Month: " + date.getMonth() + "<br>Year: " + date.getFullYear();
$("#mydate4").html("Set as September (8) but set day before month<br><br>" + formatDate(date) + breakdown);
date.setFullYear(1972);
date.setMonth(9); //October (month-1)
date.setDate(24);
var breakdown = "<br>Day: " + date.getDate() + "<br>Month: " + date.getMonth() + "<br>Year: " + date.getFullYear();
$("#mydate2").html("Set as October (9)<br><br>" + formatDate(date) + breakdown);
var date = new Date();
date.setFullYear(1972);
date.setMonth(7); //Aug (month-1)
date.setDate(24);
var breakdown = "<br>Day: " + date.getDate() + "<br>Month: " + date.getMonth() + "<br>Year: " + date.getFullYear();
$("#mydate3").html("Set as August (7)<br><br>" + formatDate(date) + breakdown);
}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
September 1972 has only 30 days. If your current local day is 31, the Date will return October 1972 when you specify September before you specify the day of the month.
<br><br><div id="mydate" style="color: red;">
</div>
<br><br>
<div id="mydate4">
</div><br><br>
<div id="mydate2">
</div>
<br><br>
<div id="mydate3">
</div>
回答by Giovanni
I just resolved this way:
我只是这样解决的:
Example
例子
var d = new Date('2013/01/31');
m = d.getMonth();
month = m*100/100 + 1;
Var monthcontains the correct value.
Var month包含正确的值。

