In theory, there isn't any difference between the theory and practice. In practice, there is.

felgall

01-13-2012, 07:34 AM

I'm not sure that is correct for the question posed.
OP wanted dd/mm/yyyy not mm/dd/yyyy format.

Data passed to Date as a string is in dd/mm/ccyy format for most of the world. It only defaults to mm/dd/ccyy if you configure your operating system to use that format - which you'd only do if you live in North America as that's the only place that uses that format.

Presumably since the OP wants dd/mm format they don't live in North America and so the Date object on their computer will be expecting dd/mm - the same as it does for most people.

Also where does the 'y' come from in your code.
Probably should be 'b' instead.

Yes, I renamed the field part way through writing the code and forgot to change all those references. So all those y references should be b.

jmrker

01-13-2012, 02:04 PM

Data passed to Date as a string is in dd/mm/ccyy format for most of the world. It only defaults to mm/dd/ccyy if you configure your operating system to use that format - which you'd only do if you live in North America as that's the only place that uses that format.

Presumably since the OP wants dd/mm format they don't live in North America and so the Date object on their computer will be expecting dd/mm - the same as it does for most people.

...

I guess I did not realize that as, yes I live in NA, and you seem to be in Australia. I'll bow to your knowledge of world-wide settings. :o

felgall

01-14-2012, 12:11 AM

I guess I did not realize that as, yes I live in NA, and you seem to be in Australia. I'll bow to your knowledge of world-wide settings. :o

I just did some further testing and it seems that JavaScript does not do quite what I expected in this instance. Even testing with a date where it is obvious from the values which parts of the date are which defining a new Date in my browser passing it '14/01/2012' it set the date to 1st February 2013 instead of the correct date. So it seems that I was wrong in assuming that Date.parse would do the logical thing and assume that whichever of the two values is over 12 is the day and that if they are both under 12 that it would examine the browser and operating system settings.

So it looks like you are correct and the date would be assumed to be mm/dd/yyyy in all cases where the year doesn't come first - even where the values make it obvious that it is intended to be dd/mm/yyyy.

jmrker

01-14-2012, 12:37 AM

I just did some further testing and it seems that JavaScript does not do quite what I expected in this instance. Even testing with a date where it is obvious from the values which parts of the date are which defining a new Date in my browser passing it '14/01/2012' it set the date to 1st February 2013 instead of the correct date. So it seems that I was wrong in assuming that Date.parse would do the logical thing and assume that whichever of the two values is over 12 is the day and that if they are both under 12 that it would examine the browser and operating system settings.

So it looks like you are correct and the date would be assumed to be mm/dd/yyyy in all cases where the year doesn't come first - even where the values make it obvious that it is intended to be dd/mm/yyyy.

Thanks for the further testing and advising me that I am not just another "(The) Ugly American"