---
title: "@Html.DisplayFor - DateFormat (“mm/dd/yyyy”)"  
description: "@Html.DisplayFor - DateFormat (“mm/dd/yyyy”)"  
author: "Anonymous User"  
published: 2015-04-15  
updated: 2015-04-15  
canonical: https://www.mindstick.com/forum/23102/html-displayfor-dateformat-mm-dd-yyyy  
category: ".net"  
tags: ["c#"]  
reading_time: 2 minutes  

---

# @Html.DisplayFor - DateFormat (“mm/dd/yyyy”)

I have the following razor code that I want to have mm/dd/yyyy [date format](https://www.mindstick.com/forum/1976/date-format-comparison-issue):

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](https://www.mindstick.com/forum/12949/how-to-validate-if-a-datetime-field-is-not-null-empty)? type

I have tried something like this and got this error:

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

[Additional information](https://www.mindstick.com/forum/160484/error-failed-to-launch-debug-adapter-additional-information-may-be-available-in-the-output-window): [Templates](https://www.mindstick.com/articles/12874/crucial-benefits-of-using-powerpoint-templates-in-your-business-presentations) can be used only with field access, [property](https://www.mindstick.com/blog/205/property-notification-in-c-sharp) access, single-[dimension](https://www.mindstick.com/articles/1508/dimension-and-metrics-in-google-analytics-api-using-asp-dot-net-mvc) [array index](https://www.mindstick.com/interview/1100/why-array-index-starts-from-zero), or single-[parameter](https://www.mindstick.com/blog/450/parameter-class-in-c-sharp) custom indexer expressions.

Tried this:

@Html.DisplayFor(Model => Model.AuditDate.[ToString](https://www.mindstick.com/interview/2259/what-is-the-difference-between-tostring-convert-tostring)("mm/dd/yyyy"))

No overload for method 'ToString' takes 1 [arguments](https://answers.mindstick.com/qa/92535/what-are-the-different-types-of-arguments)

## Replies

### Reply by Mark M

If you use DisplayFor, then you have to either define the format via the DisplayFormat attribute or use a custom display template.

[DisplayFormat(DataFormatString = "{0:MM/dd/yyyy}")]

public DateTime? AuditDate { get; set; }

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

```
@model DateTime?if (Model.HasValue){    @Model.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(DateType.Date)]public DateTime? AuditDate { get; set; }
```

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

```
@if (Model.AuditDate.HasValue){   
@Model.AuditDate.Value.ToString("MM/dd/yyyy")}
```


---

Original Source: https://www.mindstick.com/forum/23102/html-displayfor-dateformat-mm-dd-yyyy

Copyright © MindStick Software Pvt. Ltd. This Markdown version is provided for developers, AI systems, and offline reading.
