Cameron
Cameron

Reputation: 28783

ASP.NET MVC date format

I have the following code: <%= Html.Encode(String.Format("{0:g}", item.startDate)) %>

It outputs something like 01/01/2011 00:00 but I would to ONLY show the date and not the time! How can I do this? Thanks

Upvotes: 1

Views: 2137

Answers (3)

Erik Funkenbusch
Erik Funkenbusch

Reputation: 93424

<%= item.startDate.ToShortDate() %>

The above will take into account the current Locale and format it as the locale dictates. There is also no point in using Html.Encode() since it's impossible for the output of the date function to contain anything dangerous that needs encoding.

Upvotes: 4

Richard Schneider
Richard Schneider

Reputation: 35477

<%= Html.Encode(item.StartDate.ToString("dd/MM/yyy")) %>

Upvotes: 1

Bala R
Bala R

Reputation: 108927

Try

<%= Html.Encode(String.Format("{0:d}", item.startDate)) %>

Upvotes: 4

Related Questions