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

@html.displayfor date format in mvc 5
format date in mvc razor view
@html.editorfor date format
how to display only date from datetime in mvc view
c# date format
html.displayfor currency format
html tostring date format
displayformat(dataformatstring

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

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

I have tried number of different approaches but none of that approaches works in my situation

my AuditDate is a DateTime? type

I have tried something like this and got this error:

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

Additional information: Templates can be used only with field access, property access, single-dimension array index, or single-parameter custom indexer expressions.

Tried this:

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

No overload for method 'ToString' takes 1 arguments

If you use DisplayFor, then you have to either define the format via the DisplayFormat attribute or use a custom display template. (A full list of preset DisplayFormatString's can be found here.)

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

Or create the view Views\Shared\DisplayTemplates\DateTime.cshtml:

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

That will apply to all DateTimes, though, even ones where you're encoding the time as well. If you want it to apply only to date-only properties, then use Views\Shared\DisplayTemplates\Date.cshtml and the DataType attribute on your property:

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

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

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

How to format dateTime data in @Html.DisplayFor ?, Hi, in my MVC web details view, I have this line code: @Html.DisplayFor(model =​> model.StartDate, new { Class = "TableField" }) The data  Re: @Html.DisplayFor - DateFormat (“mm/dd/yyyy”) Posted on 5 years ago If you use DisplayFor, then you have to either define the format via the DisplayFormat attribute or use a custom display template.

If you are simply outputting the value of that model property, you don't need the DisplayFor html helper, just call it directly with the proper string formatting.

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

Should output

Audit Date: 1/21/2015

Lastly, your audit date could be null, so you should do the conditional check before you attempt to format a nullable value.

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

Googling the error that you are getting provides this answer, which shows that the error is from using the word Model in your Html helpers. For instance, using @Html.DisplayFor(Model=>Model.someProperty). Change these to use something else other than Model, for instance: @Html.DisplayFor(x=>x.someProperty) or change the capital M to a lowercase m in these helpers.

Html.DisplayFor - DateFormat (“mm/dd/yyyy”), I didn’t use @html.DisplayFor() .Instead of this I use @Convert.ToString(​string.Format("{0:dd/MM/yyyy}", item.chekin_on_after)). And it works perfectly. Format date and time in Html.DisplayFor() I had a hard time formatting my DateTime entry in my application. Usually, when razor displays DateTime properties in the view, it follows the format MM/dd/yyyy hh:mm:ss.

I have been using 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>

[SOLVED], So I tried to include a date format like so: @Html.DisplayFor(modelItem => item.​TranDate, "{0:MM/dd/yyyy}") This didn't work, but after looking at  I work in MVC5 and EntityFramework for displaying data into View. right now in my view data format is :- 10/22/2014 12:00:00 AM But i want only date :- 22/10/2014 in this format

@ChrisPratt's answer about the use of Display Template is wrong. The correct code to make it work is:

@model DateTime?

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

That's because .ToString() for Nullable<DateTime> doesn't accept Format parameter.

Date formatting for the MVC “DisplayFor” tag, I have the following razor code that I want to have mm/dd/yyyy date format: Audit Date: @Html.DisplayFor(Model => Model.AuditDate). Format date and time in Html.DisplayFor() I had a hard time formatting my DateTime entry in my application. Usually, when razor displays DateTime properties in the view, it follows the format MM/dd/yyyy hh:mm:ss.

I implemented the similar thing this way:

  1. Use TextBoxFor to display date in required format and make the field readonly.
@Html.TextBoxFor(Model => Model.AuditDate, "{0:dd-MMM-yyyy}", new{@class="my-style", @readonly=true})

2. Give zero outline and zero border to TextBox in css.

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

And......Its done :)

Html.DisplayFor - DateFormat (“mm/dd/yyyy”), Finally I found the way to display Date Format. Instead of using @Html.DisplayFor​, I used, @Convert.ToString(string.Format("{0:dd/MMM/yyyy}",  Using @Html.DisplayFor() is just extra work unless you are specifying a template, or need to use something that is built on templates, like iterating an IEnumerable<T>. Creating a template is simple enough, and can provide a lot of flexibility too.

DisplayFor date format mvc 5, Tengo el siguiente código de afeitar que quiero tener en formato de fecha mm/dd​/yyyy:Audit Date: @Html.DisplayFor(Model => Model.AuditDate) He probado  @Html.DisplayFor(x => x.SomeProperty) will render the default template for the given type. For example, if you have decorated your view model property with some formatting options, it will respect those options: [DisplayFormat(DataFormatString = "{0:yyyy-MM-dd}")] public DateTime SomeProperty { get; set; }

Html.DisplayFor - DateFormat ("mm/dd/aaaa"), I have the following razor code that I want to have mm/dd/yyyy date format: Audit Date: @Html.DisplayFor(Model => Model.AuditDa Note. When the DynamicField.HtmlEncode property is true, the value of the field is HTML encoded to its string representation before the formatting string is applied.For some objects, such as dates, you might want to control how the object is displayed with a formatting string.

Only date display in MVC, in my display view date format is ("dd/MM/yyyy hh:mm") 29/06/2018 12:00 but i want to display only @Html.DisplayFor(modelItem => item. Hi all, How to convert date format(dd/MM/yyyy) in MVC3 in foreach loop Table(Same as gridview)? My code is as follows @Html.DisplayFor(modelItem = > item.created_date) Thanks,

Comments
  • I have created a displayTemplate as you suggested and when I copy and paste the code you have in template then I'm getting this error No overload for method 'ToString' takes 1 arguments
  • I think that should be [DisplayFormat(DataFormatString = "{0:MM/dd/yyyy}")]
  • [DisplayFormat(DataFormatString = "{0:dd/MM/yyyy}")] worked for Turkey thanks
  • @Mac: How do you mean? They very much do work, but perhaps just not in your specific scenario. Without more information, I cannot say what your situations is.
  • if I used @Model.AuditDate.Value.ToString("d") Additional information: Templates can be used only with field access, property access, single-dimension array index, or single-parameter custom indexer expressions.
  • @AbuHamzah - sounds like you are still trying to put this in the Html.DisplayFor helper. Don't do that. Also, are you sure that its throwing the error on that line and not some other property you are trying to use the DisplayFor helper on?
  • If I used @Model... I'm getting this error: 'Model' conflicts with the declaration System.Web.Mvc.WebViewPage<TModel>.Model'
  • @AbuHamzah Please see the last paragraph in my updated answer.
  • ... and? Is this an answer or what?
  • Yes, it is simply solution to problem from subject . I thought this might be helpful to someone.
  • OK, I'd suggest you change "I have been using this change in my code " to something specifically stating that this is a solution.
  • Simple and convenient answer, worked for me thanks!!
  • But why would you do it like that?