PDA

View Full Version : Ext.form.DateField date formats



ccypert
26 Sep 2011, 8:02 AM
Rather new to EXT at the moment and I'm trying to figure out how to create a DateField (datepicker) that when displaying the date selected from the picker will be formatted based on the date format of the OS. If using the datepicker in the US I would like m/d/Y displayed, but say if in the UK it will display d/m/Y. I cannot seem to figure out how to have the format pulled from the OS instead of hard-coding a value into the format. If I do not specify a format it always seems to display a m/d/y format.

skirtle
28 Sep 2011, 12:09 AM
I don't believe there's anything in ExtJS for this. A quick search on Google found me this:

http://stackoverflow.com/questions/85116/how-do-i-display-a-date-time-in-the-users-locale-format-and-time-offset

Thought occurs that you could use the techniques described here to reverse engineer the locale's date format (create a Date object for a known date, generate the locale-specific string and then compare it to a known list of date formats). These pages may also be some help:

http://en.wikipedia.org/wiki/Calendar_date
http://en.wikipedia.org/wiki/Date_format_by_country

skirtle
28 Sep 2011, 8:16 AM
I thought this sounded interesting so I had a go at doing it myself. My conclusion is that it isn't possible using the technique I described and I haven't been able to find an alternative.

The main problem is getting hold of a suitable locale-specific date. What we'd need is a date in a form similar to m/d/Y but with the punctuation and order localized. IE and FF come close but not quite close enough to make it straightforward. With Chrome, Safari and Opera there didn't seem to be a way to get close.

I thought maybe it would be possible to work it out from the timezone name instead (e.g. PST => USA => m/d/Y) but that proved to be both an incorrect assumption and, to make things worse, the timezone name isn't available in all browsers anyway.

My current thoughts on the matter are that, assuming you don't want the user to have to choose a format, the best option is to pick a format that is likely to be understood everywhere without necessarily being localized. Y-m-d is pretty good but I could imagine it confusing some users. Month names are less confusing, e.g. Sep 28, 2011, but then you have problems with regional names for the months. If you already have a strategy for handling language-based localization then perhaps this wouldn't be such a problem. I'd definitely avoid m/d/Y and d/m/Y because they're guaranteed to confuse entire countries.

bodyboarder20
28 Sep 2011, 9:05 AM
Does this help at all?
http://docs.sencha.com/ext-js/4-0/#!/api/Date-method-toLocaleDateString

skirtle
28 Sep 2011, 11:02 AM
Afraid not, that was the main method I was trying to use.

bodyboarder20
28 Sep 2011, 11:38 AM
Although I haven't tried - I suspect that in order for it to work - you'd have to include the appropriate locale override from the locale/* folder.... Did you try that? The question then would be - how do you automatically pick the proper locale file?

skirtle
28 Sep 2011, 12:01 PM
That method is a standard part of JavaScript, not ExtJS. Adding an Ext locale file won't affect it.

Yes, if you can pick the right locale file then everything becomes trivial but I don't see a simple way to do that either. As I mentioned previously, if the OP already has a strategy for handling language-based localization then we have options available.

bodyboarder20
28 Sep 2011, 12:19 PM
Ah - good call.