This morning I noticed a peculiar issue with the JavaScript setFullYear method.
When using the method like so:
d.setFullYear(2012,2,8);
The correct value is returned:
Thu Mar 08 2012 10:30:04 GMT+0000 (GMT Standard Time)
However if I use the parseInt method to return the integers, the date returned is incorrect:
d.setFullYear(parseInt("2012"), parseInt("02"), parseInt("08"));
returns:
Wed Feb 29 2012 10:31:30 GMT+0000 (GMT Standard Time)
It appears that the parseInt method is returning the incorrect values, but when I test it:
document.write(parseInt("2"));
Then the correct value is returned (2)
A working fiddle is here: http://jsfiddle.net/rXByJ/
Does the problem lie with parseInt or with setFullYear?
The problem is that
parseInt('08')is0. This works:http://www.ventanazul.com/webzine/articles/issues-parseint-javascript
Solution is to use the second parameter:
Or
Also see How do I work around JavaScript's parseInt octal behavior?