4

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?

Hemant Tank
  • 1,724
  • 4
  • 28
  • 56
  • Why don't set your properties for "From"/"To" in your custom search object ast strings and convert them yourself in the controller ? You know the format you will receive them (MM/dd/yyyy) so you can convert more easily. – Didier Ghys Nov 26 '11 at 19:23
  • I was looking for a more standard approach. Hopefully I'm not the only one facing such situations. Something at Model Binder level ? – Hemant Tank Nov 29 '11 at 06:39
  • 1
    Found this pretty interesting [article](http://www.hanselman.com/blog/SplittingDateTimeUnitTestingASPNETMVCCustomModelBinders.aspx) about Model Binders. And this [other one](http://dukelupus.wordpress.com/2011/05/02/generic-helper-for-asp-net-mvc-model-binding/) and also [this post on SO](http://stackoverflow.com/questions/2356601/custom-datetime-model-binder-in-asp-net-mvc) – Didier Ghys Nov 30 '11 at 10:48

1 Answers1

2

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

ghusse
  • 3,200
  • 2
  • 22
  • 30
  • It's a standard feature of jQuery UI datepicker. Just try it, and use the hidden field value in your backend code. – ghusse Dec 23 '11 at 21:06
  • My application is a global web site. So how can I make sure its displaying properly in all locations? ie when my site is opened from US they should see it as mm/dd/yyy (their system's date format) when its from UK ,format will be dd/mm/yyyy(again depends on their system) – Joy George Kunjikkuru Aug 07 '13 at 16:09
  • Please create a new question on stackoverflow for your problem. – ghusse Aug 09 '13 at 12:48