1

I'm using a Date in a ASP.NET Form.

I want to show the date with french format (day/month/year) in the UI and get it in the controler with model binding work with this.

I've this model property:

[DisplayFormat(DataFormatString = "{dd/MM/yyyy}", ApplyFormatInEditMode = true)]
        public DateTime dateAudit { get; set; }

That I initiate as DateTime.Now and that show in the form:

@Html.TextBox("dateAudit", String.Format("{0:d}", Model.dateAudit.ToShortDateString(), new { @class = "datefield" }))

yeah I've put this in config too:

<globalization uiCulture="fr-FR" />

The issue is that the date shown is 3/9/2015 but should be 09/03/2015.

How to fix this?

Thanks in advance

clement
  • 4,204
  • 10
  • 65
  • 133
  • Do you need whole page/request to be fr-FR or just single field? – Alexei Levenkov Mar 09 '15 at 15:33
  • Are you sure your `CurrentCulture` is `fr-FR`? – Soner Gönül Mar 09 '15 at 15:39
  • 1
    You may need `culture="fr-FR"` in addition to `uiCulture`... http://stackoverflow.com/questions/11135002/currentculture-incorrectly-defaulting-to-en-us-in-asp-net – Alexei Levenkov Mar 09 '15 at 15:39
  • @AlexeiLevenkov: Correct. The UI Culture value is used for resource lookup, the Culture is used for formatting data. – Richard Mar 09 '15 at 16:52
  • @Alexei: it looks good fir dates before the 12th, but datepicker is appearing when I'm under 12th and when I press submit button, why? I removed the validation for the date... – clement Mar 09 '15 at 17:00
  • When i put back validation I see the error message that comes from "data-val-date" tag of the field – clement Mar 09 '15 at 17:06

2 Answers2

2

For user output always supply the applicable CultureInfo when calling String.Format or type.ToString.

Ie.

String.Format(new CultureInf("fr-FR")., "{0:d}", …)

Obviously better to get the right CultureInfo for the user, and re-use the instance.

For a client application (WPF, WinForms, ...) the defaults should be right (ie. CurrentCulture.CurrentCulture); but for the web you need to check the user's preferred locale from the HTTP request's headers (I do this in begin request, with caching of the conversion to save creating huge numbers of CultureInfo objects: cf. flyweight pattern).

Richard
  • 106,783
  • 21
  • 203
  • 265
1

Since you using d standard date and time format and ToShortDateString (which they are identical), looks like your CurrentCulture's ToShortDateString property is M/d/yyyy and that's why you get this representation.

As a solution, you can use .ToString() method to format it like;

Model.dateAudit.ToString("dd/MM/yyyy", CultureInfo.InvariantCulture)

or you can escape / character like;

Model.dateAudit.ToString("dd'/'MM'/'yyyy")
Soner Gönül
  • 97,193
  • 102
  • 206
  • 364
  • Thanks for your reply @Soner, but with the use of this, my model data binder is lost, when I select 04/03/2015 (4th of march), he understands 3 of april, and I can't select date after the 12 of the month. – clement Mar 09 '15 at 15:43
  • @clement Hmm, yeah, it might be. Richard's answer seems more logical in such a case. – Soner Gönül Mar 09 '15 at 15:46