I have the following JavaScript: http://jsfiddle.net/5Hapw/
When selecting a date, a variable will alert to say the timestamp - however, on the 8th and 9th of any month, the timestamp is always wrong.
Can anyone see where I have gone wrong?
Thanks in advance.
Your problem is in parsing a string into a number. If you don't specify a radix, and the string starts with a zero, it will be parsed as an octal number:
parseInt("017") === 15
parseInt("08") === 0 // because 8 is not a valid digit in octal numbers
The solution is to specify the radix 10:
parseInt("08", 10) === 8
Updated fiddle: http://jsfiddle.net/Guffa/5Hapw/6/
If you don't pass a second parameter to parseInt (the base) javascript will guess at it. It's probably guessing wrong. Pass 10 in as the second param to all your parseInt calls (you can get rid of the code that truncates the zero from the month - incidentally adding this code to truncate the zero from the day of month would also work)
Updated fiddle: http://jsfiddle.net/5Hapw/4/
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With