Gogo Dev
Gogo Dev

Reputation: 126

Display a formatted date of CurrentUICulture in EditorFor()

So I've been trying to "convert" a property of type DateTime to a formatted localized string to then display in a date input field.

My model's property:


[Display(Name = "StartingDate", ResourceType = typeof(Resources.Resource))]
public DateTime StartingDate { get; set; }

I've tried it with TextBoxFor() first, as you can easily pass your format as a parameter.

@Html.TextBoxFor(model => model.StartingDate, "{0:" + CultureInfo.CurrentUICulture.DateTimeFormat.ShortDatePattern + "}")

Obviously it's a plain TextBox and not an Editor, which is not my goal.

I'v also tried to use EditorFor() / input of type "date".

Normal input tag:

<input type="date" value="@Model.StartingDate.ToString("yyyy-MM-dd", CultureInfo.CurrentUICulture)" name="StartingDate" />

EditorFor:

@Html.EditorFor(model => model.StartingDate, new { @value = Model.StartingDate.ToString("{0:d}", CultureInfo.CurrentUICulture) })

In both cases, the value is still displayed as "MM/dd/yyyy", though my CurrentUICulture would theoretically display it as "dd/MM/yyyy".

Any help is welcomed, if you need more code or some other information, I'll gladly provide it to you.

Upvotes: 0

Views: 34

Answers (0)

Related Questions