@ Html.DisplayFor - DateFormat ("mm / dd / yyyy")

I have the following razor code that I want to have the mm/dd/yyyy date format:

 Audit Date: @Html.DisplayFor(Model => Model.AuditDate) 

I tried several different approaches, but none of these approaches work in my situation

is my AuditDate a DateTime? type DateTime?

I tried something like this and got this error:

 @Html.DisplayFor(Model => Model.AuditDate.Value.ToShortDateString()) 

Additional Information: Templates can only be used with access to a field, access to resources, an index of a one-dimensional array, or one-parameter custom indexer expressions.

Tried this:

 @Html.DisplayFor(Model => Model.AuditDate.ToString("mm/dd/yyyy")) 

No overload for 'ToString' method takes 1 argument

+70
asp.net-mvc
Jan 23 '15 at 16:57
source share
14 answers

If you are using DisplayFor , you need to either define the format using the DisplayFormat attribute, or use a custom display template. (A full list of predefined DisplayFormatString can be found here .)

 [DisplayFormat(DataFormatString = "{0:d}")] public DateTime? AuditDate { get; set; } 

Or create a Views\Shared\DisplayTemplates\DateTime.cshtml :

 @model DateTime? @if (Model.HasValue) { @Model.Value.ToString("MM/dd/yyyy") } 

This applies to all DateTime s, albeit to those where you also encode time. If you want it to apply only to date-only properties, use the Views\Shared\DisplayTemplates\Date.cshtml and DataType for your property:

 [DataType(DataType.Date)] public DateTime? AuditDate { get; set; } 

The final option is to not use DisplayFor and instead display the property directly:

 @if (Model.AuditDate.HasValue) { @Model.AuditDate.Value.ToString("MM/dd/yyyy") } 
+132
Jan 23 '15 at 17:06
source share

If you simply output the value of this model property, you don’t need the HTML helper for DisplayFor , just call it directly with the correct string formatting .

Audit Date: @Model.AuditDate.Value.ToString("d")

Must output

Audit Date: 1/21/2015

Finally, your audit date may be null, so you must do a conditional check before trying to format a nullable value.

 @if (item.AuditDate!= null) { @Model.AuditDate.Value.ToString("d")} 

When you receive an error message, you will receive this answer , which shows that the error is related to the use of the word Model in your HTML helpers. For example, using @Html.DisplayFor(Model=>Model.someProperty) . Change them to use something other than Model , for example: @Html.DisplayFor(x=>x.someProperty) or change the capital letter M to lowercase m in these helpers.

+18
Jan 23 '15 at 17:02
source share

@ChrisPratt incorrect answer about using display template. The correct code to work is:

 @model DateTime? @if (Model.HasValue) { @Convert.ToDateTime(Model).ToString("MM/dd/yyyy") } 

This is because .ToString() for Nullable<DateTime> does not accept the Format parameter .

+5
Sep 03 '15 at 0:29
source share

I implemented like this:

  • Use TextBoxFor to display the date in the desired format and enter a read-only field.
 @Html.TextBoxFor(Model => Model.AuditDate, "{0:dd-MMM-yyyy}", new{@class="my-style", @readonly=true}) 

2. Give a null scheme and a null frame in a TextBox in css.

 .my-style { outline: none; border: none; } 

And ...... Done :)

+4
May 26 '16 at 20:05
source share

It was enough for me to use

 [DataType(DataType.Date)] [DisplayFormat(DataFormatString = "{0:dd/MM/yyyy}")] public DateTime StartDate { set; get; } 
+3
Nov 01 '16 at 12:35
source share

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

+2
Jan 25 '17 at 15:45
source share

I used this change in my code:

old code:

  <td> @Html.DisplayFor(modelItem => item.dataakt) </td> 

new:

 <td> @Convert.ToDateTime(item.dataakt).ToString("dd/MM/yyyy") </td> 
+2
Oct 25 '17 at 8:18
source share

You can use the [ DisplayFormat ] attribute in your view model because you want to apply this format to the entire project.

 [DisplayFormat(ApplyFormatInEditMode = true, DataFormatString = "{0:dd/MM/yyyy}")] public Nullable<System.DateTime> Date { get; set; } 
+1
Sep 20 '19 at 3:14
source share

Maybe just try

 @(Model.AuditDate.HasValue ? Model.AuditDate.ToString("mm/dd/yyyy") : String.Empty) 

You can also use many types of string format, for example .ToString ("dd MMM, yyyy") .ToString ("d"), etc.

0
Jan 23 '15 at 17:04
source share

I had a similar problem with my controller, and here is what worked for me:

 model.DateSigned.HasValue ? model.DateSigned.Value.ToString("MM/dd/yyyy") : "" 

"DateSigned" is the value from my model. The line reads if the value of the model has a value and then formats the value, otherwise nothing is displayed.

Hope that helps

0
Aug 25 '17 at 2:51 on
source share

This is the best way to get a simple date string:

  @DateTime.Parse(Html.DisplayFor(Model => Model.AuditDate).ToString()).ToShortDateString() 
0
Apr 11 '18 at 7:33
source share

In the view, replace this:

 @Html.DisplayFor(Model => Model.AuditDate.Value.ToShortDateString()) 

FROM:

 @if(@Model.AuditDate.Value != null){@Model.AuditDate.Value.ToString("dd/MM/yyyy")} else {@Html.DisplayFor(Model => Model.AuditDate)} 

Explanation: If the value of AuditDate is not zero, then it will format the date in dd / MM / yyyy, otherwise leave it as it is because it does not matter.

0
Apr 23 '19 at 5:22
source share

You can use convert

  <td>@Convert.ToString(string.Format("{0:dd/MM/yyyy}", o.frm_dt))</td> 
0
Jul 25 '19 at 8:19
source share

See this error response No overload for method 'ToString' takes 1 arguments .

You cannot format a null DateTime - you need to use the DateTime.Value property.

 @Model.AuditDate.HasValue ? Model.AuditDate.Value.ToString("mm/dd/yyyy") : string.Empty 

Tip. It is always useful to work with this material in a standard class with intellisense before putting it into the view. In this case, you will get a compilation error, which is easy to notice in the class.

-one
Jan 23 '15 at 18:18
source share



All Articles