1

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.

Community
  • 1
  • 1
Heathcliff
  • 3,048
  • 4
  • 25
  • 44
  • Why not just toggle parts of the string converting them all to European standard before comparing? – dfsq Jan 22 '13 at 13:10
  • if he could convert them to European standard,wouldn't he have to know what format it is in and could use it right away? – Moritz Roessler Jan 22 '13 at 13:13
  • Are you receiving the dates as a text input, i.e. manually via text field, or are you receiving them from a database? – Gavin Jan 22 '13 at 13:14
  • @Gavin: from manual input. – Heathcliff Jan 22 '13 at 13:14
  • Ask your users to input a date as `YYYY-MM-DD`, use individual boxes (drop down with month names) or an [``](http://www.w3.org/TR/html-markup/input.date.html)/shim. Then there should be no confusion over this. – Paul S. Jan 22 '13 at 13:48
  • @PaulS.: Thanks, but that isn't an option – Heathcliff Jan 22 '13 at 15:47

2 Answers2

1

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 Date Object 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 ->

var year1 = 2013; //Suppose those variables will be handle by some user input method
var year2 = 2013;

var month1 = 0; //January (Months are zero based in javascripts Date Object)
var month2 = 3; //April

var day1 = 3;
var day2 = 1


var date1 = new Date(year1,month1,day1);
var date2 = new Date(year2,month2,day2);

console.log(date2.getTime() > date1.getTime()); //true

And avoid that Problem at all

BenMorel
  • 34,448
  • 50
  • 182
  • 322
Moritz Roessler
  • 8,542
  • 26
  • 51
  • I AM currently using a DatePicker to fill that input. But how can I check which one is the month and which the day? – Heathcliff Jan 22 '13 at 13:29
  • What kind of Datepicker is this, isn't it you who is in controle over what part of the datepicker is the Month and the Day ? If so you could just label them as such. _Sry, but thats left unclear in your question._ – Moritz Roessler Jan 22 '13 at 13:34
  • 1
    Remember that `month?` should be decremented by 1, as the `month` parameter is zero-based. – Marcel Korpel Jan 22 '13 at 13:48
  • Oh thx, totally missed to Point that out -> I'll update the Answer – Moritz Roessler Jan 22 '13 at 13:50
  • @Glutamat: DatePicker writes a date in the input in text form, but how do I know what format it used, if "DD/MM/YYYY" or "MM/DD/YYYY"? Where does it leave it stated? – Heathcliff Jan 22 '13 at 13:55
  • I solved it by sending it to server-side to check. I have more tools there to check the date. I'll give you the Accept because you answer the question I made precisely with the first paragraph. – Heathcliff Jan 23 '13 at 11:58
1

the convert-method in the given link checks for the constructor-type. if you are giving the convert-method your data as a string, it will instantiate a Date-Object with the standard-date constructor which needs to be an RFC 2822 compliant timestamp.

of course "03/02/2013" is a valid and compliant timestamp. it will always return march 2nd. how will you ever recognize if the european format is meant???

you could write some code which uses the "toLocaleDateString"-method, to do a compare. you would then know if your app is currently viewed on a european machine. in your example:

new Date("03/02/2013").toLocaleDateString()

will return "2.3.2013" in europe

a basic check (only if dots exists, could be done like that):

new Date("03/02/2013").toLocaleDateString().split('.').length === 3

if this returns true, you are probably on a european machine.

hereandnow78
  • 14,094
  • 8
  • 42
  • 48
  • That's what I wanted to know. Is this method going to check for the computer's location, or will I have to do that? And if so, how? – Heathcliff Jan 22 '13 at 13:23
  • Converts a date to a string, returning the "date" portion using the operating system's locale's conventions, see this: https://developer.mozilla.org/en-US/docs/JavaScript/Reference/Global_Objects/Date/toLocaleDateString – hereandnow78 Jan 22 '13 at 13:25
  • This method is not reliable; I'm in the UK so my machine's date today is 22/01/2013 (day/month/year), now using your method, `new Date("03/02/2013").toLocaleDateString()`, Chrome (24) gives `"3/2/2013"` (month/day/year) and Firefox (16, 17, 18) gives `"02 March 2013"` (month as name). – Paul S. Jan 22 '13 at 13:41
  • ok, so he is not sure if he is in europe, but the main goal is still achieved. he knows that your data-format is month/day/year, and he can use the constructed data-object as it is – hereandnow78 Jan 22 '13 at 13:46
  • @hereandnow78 ..my date format is not month/day/year is one of the points I'm raising. – Paul S. Jan 22 '13 at 13:50