After some digging, and I finished installing Thread CurrentCulture to CultureInfo ("en-US") in the controller action method:
Thread.CurrentThread.CurrentCulture = new System.Globalization.CultureInfo("en-US");
Here are a few other options if you want to have this setting for each view.
About the value of the CurrentCulture property:
The CultureInfo object that this property returns, along with its associated objects, defines the default format for dates, times, numbers, currency values, text sort order, casing conventions and string comparisons.
Source: MSDN CurrentCulture
Note. The previous parameter of the CurrentCulture property is probably optional if the controller is already working with CultureInfo("en-US") or similar if the date format is "MM/dd/yyyy" .
After setting the CurrentCulture property, add a code block to convert the date to the "M/d/yyyy" format in the view:
@{ //code block var shortDateLocalFormat = ""; if (Model.AuditDate.HasValue) { shortDateLocalFormat = ((DateTime)Model.AuditDate).ToString("M/d/yyyy"); //alternative way below //shortDateLocalFormat = ((DateTime)Model.AuditDate).ToString("d"); } } @shortDateLocalFormat
Above @shortDateLocalFormat variable @shortDateLocalFormat formatted ToString("M/d/yyyy") works. If you use ToString("MM/dd/yyyy") , like the first time, then you end up with a leading null problem . Also, as recommended by Tommy ToString("d") , also works. Actually "d" stands for “Short date pattern” and can be used with various culture / language formats.
I think the code block above can also be replaced with some cool helper method or similar.
for example
@helper DateFormatter(object date) { var shortDateLocalFormat = ""; if (date != null) { shortDateLocalFormat = ((DateTime)date).ToString("M/d/yyyy"); } @shortDateLocalFormat }
can be used with this helper call
@DateFormatter(Model.AuditDate)
Refresh , I found out that an alternative way to do the same when DateTime.ToString (String, IFormatProvider) . When this method is used, there is no need to use the Thread s CurrentCulture property. CultureInfo("en-US") is passed as the second argument → IFormatProvider in the DateTime.ToString(String, IFormatProvider) method.
Modified helper method:
@helper DateFormatter(object date) { var shortDateLocalFormat = ""; if (date != null) { shortDateLocalFormat = ((DateTime)date).ToString("d", new System.Globalization.CultureInfo("en-US")); } @shortDateLocalFormat }
.NET Fiddle