Why this is giving different month
The following code below:
var unixDate = new Date('07/28/2010');
var unixMonth = unixDate.getMonth();
var unixDay开发者_Go百科 = unixDate.getDate();
var unixYear = unixDate.getFullYear();
alert(filterDate.value);
alert(unixMonth);
alert(unixDay);
alert(unixYear);
should give me month 07 but it alerts 06.... why's that?
Months are zero based. Just do +1
. See also Date.getMonth()
at MDC:
The value returned by getMonth is an integer between 0 and 11. 0 corresponds to January, 1 to February, and so on.
The months are 0-based, 0=January
http://www.w3schools.com/jsref/jsref_getMonth.asp
.getMonth
returns a zero indexed month. So, 0 = January, and 11 = December.
Use:
var unixMonth = unixDate.getMonth() + 1;
.getMonth
returns a zero indexed month.
0 = January
11 = December
More Info
The getMonth() method returns the month (from 0 to 11) for the specified date, according to local time.
Note: January is 0, February is 1, and so on.
My guess would be that 0 = January and thus your enumeration is slightly off.
精彩评论