2

I would like to display only the date on the page.

SQL column has type date.

Model

public System.DateTime EndDate { get; set; }

View

@Html.DisplayFor(modelItem => item.EndDate.Date)

This gives me no warnings or errors, however the time is still displaying on the page.

kingdango
  • 3,979
  • 2
  • 26
  • 43
AM_Hawk
  • 661
  • 1
  • 15
  • 33

3 Answers3

4

DateTime.Date returns a new instance of DateTime with the time component set to midnight (rather than removing the Time completely), it's only useful for normalizing date/time values and is not intended for display purposes.

Anyway, you don't need DisplayFor, you can render the date directly:

@item.EndDate.ToString("yyyy-MM-dd")
Dai
  • 141,631
  • 28
  • 261
  • 374
3

You can use theDate.ToShortDateString() or use a custom date/time string format.

The benefit of ToShortDateString() is that it is CULTURALLY SENSITIVE making your application more accessible.

If you want to use the Html.DisplayFor template helper then you may want to build a ShortDateTime.cshtml display template. Learn more about that here: https://stackoverflow.com/a/6001836/941058

Community
  • 1
  • 1
kingdango
  • 3,979
  • 2
  • 26
  • 43
1

Try the ToString() date formatters:

http://msdn.microsoft.com/en-us/library/8kb3ddd4.aspx

Example:

@Html.DisplayFor(modelItem => item.EndDate.Date.ToString("MMMM dd, yyyy")
Haney
  • 32,775
  • 8
  • 59
  • 68