user1464139
user1464139

Reputation:

How can I reformat the way a data appears inside an ASP MVC Razor page?

I have the following code:

<li>Reviewed: @Model.Modified</li>

and a model that looks like this:

    [DisplayFormat(DataFormatString = "{0:dd/MM/yyyy}")]
    public DateTime? Modified { get; set; }

However when displayed then the date appears as follows:

REVIEWED: 8/1/2012 2:42:34 PM

Is there a simple way that I could change the way the date displays so it appears as just 8/1/2012 2PM ?

Upvotes: 0

Views: 67

Answers (3)

Christofer Eliasson
Christofer Eliasson

Reputation: 33865

You can use DateTime.ToString(format) in your view as well, to format the date the way you want.

So in your case, something like:

@Model.Modified.ToString("dd/MM/yyyy htt")

Upvotes: 2

MVCKarl
MVCKarl

Reputation: 1295

Try this

[DisplayFormat(DataFormatString = "{0:dd/MM/yyyy htt}")]
   public DateTime? Modified { get; set; }

This website provides easy to understand information on setting date/time formats

Date Formatting C#

Upvotes: -1

Zabavsky
Zabavsky

Reputation: 13640

You can use DisplayExtensions.DisplayFor method:

@Html.DisplayFor(model => model.Modified)

This helper works with data annotations and display templates.

Upvotes: 1

Related Questions