How can I set the date format in jQuery UI's datepicker according to the user's locale?
I get source html:
<script type="text/javascript">
$(function() {
$(".datefield2").datepicker($.datepicker.regional['de']);
});
</script>
<form action="/Home/TestDate" method="post"> <input class="datefield2 hasDatepicker valid" name="date" value="21.12.2012" type="text" id="date">
<input type="submit" value="send">
</form>
If a select a date in the datepicker I get 12/21/2012
, however I need 21.12.2012
.
Since there doesn't seem to be a readily available locale information, I decided to use a bit different approach: I only use datepicker for choosing the date and retrieving the date as such from it, but for display I use an ordinary div and Date.toLocaleDateString(). This way I needn't bother with retrieving the format or worse yet - providing them all myself.
JSFiddle
HTML:
JS:
The JQuery documentation on the web seems to play hide&seek for where the i18n files for the datepicker are see e.g.
Where are the i18n files of jQuery UI datepicker >= 1.11.0
As of this posting the link:
https://raw.githubusercontent.com/jquery/jquery-ui/master/ui/i18n/datepicker-de.js
seems to be valid. If i included it it didn't work by itself. I ended up using http://ajax.googleapis.com/ajax/libs/jqueryui/1.11.1/i18n/jquery-ui-i18n.min.js which is not the same version as the 1.12 jquery I am using but it seems to work.
Please note I am using iso date format 2017-03-18 as yy-mm-dd here instead of the german locale style.
Use the
dateFormat
option to change to a custom formatThe real issue is that you need to manually include the localization files.
They can be found at https://github.com/jquery/jquery-ui/tree/master/ui/i18n
The one specific for german is https://github.com/jquery/jquery-ui/blob/master/ui/i18n/datepicker-de.js
Demo (with full localization) can be seen at http://jsfiddle.net/gaby/h07ny9ep/
You can set the date format like this: