jqueryui datepicker dd/MM/yy culture related issue

ぃ、小莉子 提交于 2021-02-16 08:46:12

问题


I'm using a jQueryUI datepicker for a simple "From" & "To" date range filter in my ASP.Net MVC web-app. Works fine until I hit a machine which had its datetime format (in the Regional Settings) set to "dd-MM-yy".

In my controller I've a postback action which accepts a custom search object as a parameter which is then passed to the search function. This custom object has the properties for "To" & "From" dates and as usual the binding takes place automatically. Works normal on a machine with date format "MM/dd/yy" but if I set it to "dd/MM/yy" in the regional settings then it is unable to parse the date.

The easiest solution could be to change the datepicker's format to "dd-MMM-yyyy" a non-culture specific 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 at the datepicker or controller level?


回答1:


You can use an 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'll find documentation about jquery ui date format here



来源:https://stackoverflow.com/questions/7968083/jqueryui-datepicker-dd-mm-yy-culture-related-issue

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!