0

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?

Eregrith
  • 4,263
  • 18
  • 39
  • Duplicate?: http://stackoverflow.com/questions/23566747/datetime-parse-not-reversing-month-and-day-based-on-thread-culture – peinearydevelopment Jun 15 '15 at 17:03
  • Maybe related but i'm not calling DateTime.Parse myself so in no way it's a duplicate. I need an answer based on MVC automated model binding which is not doing its job properly – Eregrith Jun 15 '15 at 17:06
  • Can you tell what is your `CurrentUICulture` value? – kamil-mrzyglod Jun 15 '15 at 20:13
  • Probably `fr-FR` culture has `dd/MM/yyyy` as a standard date and time format, not `MM/dd/yyyy`. – Soner Gönül Jun 16 '15 at 06:08
  • @SonerGönül that's the thing, `dd/MM/yyyy` is supposed to parse `"12/06/2015"` as June 12th, but it parses it as December 6th. – Eregrith Jun 16 '15 at 06:54
  • @Eregrith How do you parse it exactly? With the Javascript code in your question or C#? I'm not too much familiar with JS. – Soner Gönül Jun 16 '15 at 06:57
  • @SonerGönül No, the MVC model binding parses it, as I said in my question – Eregrith Jun 16 '15 at 06:57
  • @Kamo `System.Threading.Thread.CurrentThread.CurrentCulture` is `{fr-FR}` as well in `OnAuthorization`. `CurrentUICulture` is `{fr-FR}` as well at this state. – Eregrith Jun 16 '15 at 06:58

1 Answers1

0

Try to make sure your culture is set before the model binding occurs. If you check your System.Threading.Thread.CurrentThread.CurrentCulture setting inside your controllers action than it's posiible that this was set after the date was parsed by your model binder.

You can check this by implementing IAuthorizationFilter.

  • `System.Threading.Thread.CurrentThread.CurrentCulture` is `{fr-FR}` as well in `OnAuthorization`. `CurrentUICulture` is `{fr-FR}` as well at this state. – Eregrith Jun 16 '15 at 06:58