Jquery datapicker localization using globalize plugin

5.2k Views Asked by At

Is it possible localize jquery datapicker using globalize plugin (https://github.com/jquery/globalize)?

I tried in this way

// get a date format 
var dt = Globalize.culture().calendar.patterns.d;
$('#dp1').datepicker({
    dateFormat : dt
});

but it does not work because datepicker and globalize plugin use two format different.

I want the date with 2 digits for the month, 2 digits for the day and 4 digits for the year, so in jquery the date format must be "dd/mm/yy". But globalize use a different date format: en-Us -> M/d/yyyy it-IT -> dd/MM/yyyy

3

There are 3 best solutions below

0
On

I bumped into many problems of those two format discrepancies. Finally jQuery guys replied me they would switch to Globalize format using. However, when... :(

0
On

In the early days of Globalize (called back then jQuery Globalize) there was specifically patched version of Datepicker available.
Frankly, I don't think it is usable anymore, but you may try to apply the same modifications to current version and the problem will be resolved. This could be done quite easy with a diff command available with many Unix-like systems (and on Windows via Cygwin for example).


Edit: API

I think it is at least good to mention, that Datepicker is offering rich API.
You can use to tailor down Datepicker's Localization. However, when it comes to date format, I am afraid that you would be forced to use some replacement to remove "excessive" y's from year field. That's something I would prefer not to do, instead I would patch Datepicker, as suggested previously, but...

1
On

Small converter:

Globalize.getPatternForDatapicker = function (pattern) {
    return this.culture().calendar.patterns[pattern || 'd'].toLowerCase().replace('yyyy', 'yy');
};

and

$('#dp1').datepicker({dateFormat : Globalize.getPatternForDatapicker()});