I’ve noticed an issue with some data I’m processing (2000+ lines of data).
The problem is very strange: the code works fine! UNTIL a specific date is reached (in this case 01/08/2011) when JavaScript fails to generate the correct date?
So, to explain: I’m taking a string in the format of ‘dd/mm/yyyy’ and doing (which doesn’t work):
var date = '01/08/2011'.split('/');var milliseconds = new Date(date[2], parseInt(date[1]) - 1, date[0]).getTime();=> 1291161600000new Date(1291161600000);=> Wed Dec 01 2010 00:00:00 GMT+0000 (GMT)
…but yet that exact code works fine with any date before the 1st August 2011?
So try again with 29/07/2011…
var date = '29/07/2011'.split('/');var milliseconds = new Date(date[2], parseInt(date[1]) - 1, date[0]).getTime();=> 1311894000000new Date(1311894000000);=> Fri Jul 29 2011 00:00:00 GMT+0100 (BST)
The only difference is the (GMT) and (BST) values returned, which suggests a locale issue. But why would that occur, and how can I fix the code to work around that?
Many thanks for any help you can give me.
You are doing octal!
Use a radix!
From the MDN Docs
parseInt(string[, radix]):