JQuery UI datepicker - can I format a date that has been localized?

I have a jQuery UI datepicker and am looking to localize the returned date and then format it to remove leading zeros.

It seems I can do one or the other. Is there a way to both localize and format it?

My sample code is:

// myDate = 01/25/2010 // myLanguage = ""; //for US english, but could be any language $('#mytxtbox').datepicker($.datepicker.regional['<%= mylanguage %>']); $('#mytxtbox').datepicker({ dateFormat: 'm/d/yy' }); var dateToSet = $.datepicker.parseDate('m/d/yy', '<%= myDate %>'); $('#mytxtbox').datepicker("setDate", dateToSet); 

in the above example, the date displayed in my text box is "01/25/2010". If I comment out the line that sets the scope, formatting works (i.e. 1/25/2010). It seems to me that localization is rewriting my "dateFormat"? Is it possible to do this?

Please note that I have included all relevant region files

+4
source share
6 answers

I ran into the same problem. My solution: (regional settings are in an array, just add some values)

 var defaults = $.datepicker.regional['<%= mylanguage %>']; defaults['dateFormat'] = 'm/d/yy'; $( "#mytxtbox" ).datepicker( defaults ); 
+1
source

setDate accepts a Date object as a region-independent parameter.

Sets the current date for datepicker. The new date can be a Date object or a string in the current date format (for example, '01 / 26/2009 '), the number of days from today (for example, +7) or a string of values ​​and periods (' y 'for years,' m ' for several months, β€œw” for weeks, β€œd” for several days, for example β€œ+ 1m + 7d”), or null to clear the selected date.

0
source

That sounds like your question . JQuery Datpicker Language

0
source

I believe this is because you already initialized the datepicker with localization as the only option. If you do not want the region to be dynamic or want to set the datepicker parameter to its initial state, try to execute both parameters in the same options object .

 $("#mytxtbox") .datepicker({ dateFormat: 'm/d/yy', $.datepicker.regional['<%= mylanguage %>'] }); 

OR if you want it to be dynamic, do something similar to the jQuery ui example .

Try formatting the date first. Then set the regional option.

 //original initialization $("#mytxtbox").datepicker({dateFormat: 'm/d/yy'}); //then set localize the date based on a change function $("ul.region").change({ $("mytxtbox").datepicker('option', $.datepicker.regional[$(this).val()]); }); 
0
source

if you perform regional and beforeShow or any other function similar to this, you might like it all in one:

 $("#mytxtbox").datepicker({dateFormat: 'dd-mm-y'}, $.datepicker.regional[ "ar" ], { beforeShow: function(input, inst) { var widget = $(inst).datepicker('widget'); widget.css('margin-left', $(input).outerWidth() - widget.outerWidth()); } }); 
0
source

It may be too late, but since I did some research on Google and I came here, I am posting my solution. I think you have an input type for datepicker, for example:

 <input id="date" type="text" name="date"> 

If you want to set the language standard according to your visitors and a predefined date format for further definition, you can initiate your datepicker as follows:

  $(function() { $('#date').datepicker(); $('#date').datepicker("option",$.datepicker.regional["<<your_variable_locale>>"]); $('#date').datepicker("option","dateFormat","dd/mm/yy"); }); 

It will change the default English format, i.e. mm / dd / yy

The documentation is not very clear how to combine parameters such as regional and date format, but it works for me in Firefox.

0
source

All Articles