I have an ajax call sending data from a datepicker as a short date to my controller's method. Inside my controller I want to use the date to filter entries in a table, and return the whole thing along with the date.
My app's culture is french (fr-FR
)
Let's use June 12th 2015
for our examples.
I encountered several problems:
The jQuery datepicker is defaulted to have dateFormat: "DD d MM yy"
, so when I pick June 12th in the date picker, the field shows "vendredi 12 juin 2015" and the JS sends "vendredi+12+juin+2015"
as my DateTime field, which yields null
inside the controller's method.
I decided to fix that by re-specifying the dateFormat
to be "dd/mm/yy"
which takes less screen space anyway.
Now, when I pick june 12th, the fields holds (and the JS sends) "12/06/2015"
which yields {06/12/2015 00:00:00}
in the controller's method with 12 as the month and 06 as the day. I checked my System.Threading.Thread.CurrentThread.CurrentCulture
to see it is {fr-FR}
, so I don't understand why "12/06/2015"
is parsed as December 6th.
I implemented IAuthorizationFilter
in my controller to see in OnAuthorization
what were the CurrentThread's culture:
Both CurrentCulture
and CurrentUICulture
are {fr-FR}
I tried turning it around, and set the date picker format to mm/dd/yyyy
which (in)correctly translates June 12th to "06/12/2015"
which yields {12/06/2015 00:00:00}
in the controller's method. This could work but then when my date is reinserted into the page, it's "12/06/2015" which is correct but interpreted by datepicker as December 12th because of the date format.
I also tried sending the value with $('#date').datepicker({ dateFormat: "mm/dd/yy "}).val()
and leave the datepicker format on the first call as "dd/mm/yy"
, but this still gives me the value formatted as "dd/mm/yy"
I had to parse and then format the date like this:
$.datepicker.formatDate("mm/dd/yy", $.datepicker.parseDate("dd/mm/yy", $('#date').val()))
In order to "reverse" the month and the day to trick the server into parsing it as June 12th.
Additionnal info: I have no <globalization ...>
in my Web.config nor do I have any custom model binder.
I could maybe try to have a SSCCE if you need it but it looks like it's hard to shrink down...
TL;DR: I have CurrentThread.CurrentCulture = {fr-FR}
displayed when I break on the first line inside my controller's method, yet my DateTime sent as "12/06/2015"
(June 12th) in the request is parsed as "06/12/2015"
(December 6th) before being passed to my controller's method.
Why does my CurrentCulture
does not seem to be taken into account when the server is parsing the date?