If you want to control the format for a specific field, use the
DisplayFormat
attribute:
DisplayFormatAttribute Class | Microsoft Docs[
^]
[DisplayFormat("dd-MMM-yyyy")]
public DateTime? PublishDate { get; set; }
(
NB: DateTime?
is a shorthand way of writing
Nullable<DateTime>
.)
If you want to override the format for all dates, create a display template in
Views\Shared\DisplayTemplates\Date.cshtml
:
@model DateTime?
@if (Model.HasValue)
{
@Model.Value.ToString("dd-MMM-yyyy")
}
and then add the
DataType
attribute to any properties which should only show the date:
[DataType(DataType.Date)]
public DateTime? PublishDate { get; set; }
If you want to turn off automatic globalization for the entire application, you can change the
web.config
file. You probably have the culture set to "auto":
<configuration>
<system.web>
<globalization culture="auto" uiCulture="auto" />
</system.web>
</configuration>
This sets the culture based on the user's preferred language.
Replace "auto" with the code of the culture you want to use. For example:
<configuration>
<system.web>
<globalization culture="en-GB" uiCulture="en-GB" />
</system.web>
</configuration>
You can even have different settings for culture and UI culture - the culture drives number and date formatting, currency symbols, sorting, etc., whereas the UI culture is primarily used to determine which
.resx
files are used to look up localized resources.
Auto Selecting Cultures for Localization in ASP.NET - Rick Strahl's Web Log[
^]