If I use the strategy to compare text-based dates shown here: Compare two dates with JavaScript.
Suppose the user inputs “03/02/2013” (the format with “/” will be respected) and I compare it to “04/01/2013”. In the European calendar, the first date is February 3rd and the second is January 1st. But in the US calendar, the first is March 2nd and the second is April 1st. For the European standard, the first date is bigger than the second, but for the US standard, the second is larger.
Does this method from the link account for dates location? I haven’t found a way to test this without my computer taking the European version, that’s why I ask.
No, the strategy there doesn’t handle Date Localization for you.
Javascripts internal Date Object does take Localization in account. But thats just depending on the Browsers/Machines Locale settings
Which does not necessarily have to match the Users Date format,sitting infront of it
And you can’t distinguish a DD/MM/YYYY from a MM/DD/YYYY format. exception for: DD > 12
without further information about the user .
You could of course use the localization from the browser/machine or the ip adress to get a location or something else,
and handle the formating depending on this information, but this could easily lead to wrong results.
I would rather make sure that the
DateObject is constructed right.For example by providing a Date Picker which lets the user choose the Year Month and Day respectively to avoid the Date ambiguity due to their format.
Because you can’t make sure what format the user, sitting at the PC is using.
e.g: I’m european and have my Browsers Locale Settings on enUS.
Therefore the
Then you can construct your Date Object like
new Date(YEAR,MONTH,Day)e.g ->
And avoid that Problem at all