This will be a short and simple post dealing with an improvement on the traditional display and editor templates normally found for MVC date display. If you are not familiar with the issue, normally a DateTime field will display the full information, including the time. Commonly this is not the desired display, we only want the date part. While you can format the display on each view, that becomes cumbersome, and it is easy to miss one. MVC has a great solution for that, called DisplayTemplates. Located in the \Views\Shared\DisplayTemplates folder they will apply to that data type site wide. So creating a DateTime.cshtml file here will be used for the display anytime the @Html.DisplayFor helper is used (the same is applied for the @Html.EditorFor and the EditorTemplates folder).
Commonly the contents of the DateTime.cshtml file is simple:
@model System.DateTime
@string.Format("{0:d}", Model)
This partial view simply formats the DateTime values using the ShortDate pattern. Simply create the file, and you are ready to do… But what if the field is initialized, but has no value? I recently had a case where the field was not null, but contained the default date of 01/01/0001. Obviously this was not the desired display. Continue on to find out the simply solution.