Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

JavaScript date to timestamp calculation doesn't work for 8th and 9th of any month

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.

like image 839
Karl Tynan Avatar asked Dec 13 '25 06:12

Karl Tynan


2 Answers

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/

like image 144
Guffa Avatar answered Dec 14 '25 20:12

Guffa


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/

like image 22
James Avatar answered Dec 14 '25 20:12

James



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!