Jqueryui datepicker dd / MM / yy cultural issue

I use jQueryUI datepicker for a simple From and To date range filter in my ASP.Net MVC web application. It works fine until I hit a machine that had a date and time format (in regional settings) set to "dd-MM-yy".

In my controller, I have a postback action that takes a custom search object as a parameter, which is then passed to the search function. This custom object has properties for the dates "To" and "From", and, as usual, the binding occurs automatically. It works fine on a machine with the date format "MM / dd / yy", but if I set it to "dd / MM / yy" in the regional settings, then it will not be able to parse the date.

The easiest solution is to change the datepicker format to "dd-MMM-yyyy" - the non-financial date format, but my client wants the selected date to be displayed in MM / dd / yyyy format.

Any clean and easy suggestion to handle this on a date or controller level?

+4
source share
1 answer

You can use a hidden field with a custom format:

HTML:

<input name="from" type="text" id="from" /> <input name="hiddenFrom" type="hidden" id="hiddenFrom" /> 

JavaScript:

 $("#from").datepicker({ altField: "#hiddenFrom", altFormat: "dd-mm-yy" // or whatever }); 

You will find the jquery ui date format documentation here

+2
source

Source: https://habr.com/ru/post/1379149/


All Articles