So basically, what I want is to format DateTime
in whole application and show it in a specific format in the whole Web App. Is there any better solution to just write ToString("{0:stringformathere}", obj)
?
Asked
Active
Viewed 864 times
1
-
possible duplicate of [How do you globally set the date format in ASP.NET?](http://stackoverflow.com/questions/300841/how-do-you-globally-set-the-date-format-in-asp-net) – Parv Sharma Jan 30 '14 at 09:38
2 Answers
1
Try to override InitializeCulture method for Web page.
Thread.CurrentThread.CurrentCulture = <some custom culture>

hardsky
- 514
- 5
- 15
0
You can create your own Editor/Display Template and reuse across the application.
Views/Shared/EditorTemplates/CustomDateTime.cshtml
@model DateTime?
@{
String modelValue = "";
var dateFormatToDisplay =
System.Threading.Thread.CurrentThread.CurrentCulture.DateTimeFormat.ShortDatePattern;
if (Model.HasValue)
{
if (Model.Value != DateTime.MinValue)
{
modelValue = Model.Value.ToString(dateFormatToDisplay);
}
}
}
@Html.TextBox("", modelValue)
In Views
@Html.EditorFor(m => m.DateOfBirth, "CustomDateTime")

Murali Murugesan
- 22,423
- 17
- 73
- 120