İsmet Alkan
İsmet Alkan

Reputation: 5447

ASP.NET MVC DisplayFormat for Hour

I have the following TimeSpan variable in my Model class:

[DisplayFormat(DataFormatString = "{0:HH-mm}", ApplyFormatInEditMode = true)]
public TimeSpan StartingHour { get; set; }

But when I call it from my View I got the error:

@Html.DisplayFor(modelItem => item.StartingHour)

System.FormatException: Input string was not in a correct format.

The value of the variable is: "18:00:00".

What should be in my DisplayFormat initialization? And please keep "get hour and minute separately from the time" advices to yourself.

Upvotes: 3

Views: 5083

Answers (2)

Rashedul.Rubel
Rashedul.Rubel

Reputation: 3584

Try using:

DataFormatString ="{0:hh\\:mm}"

Thanks.

Upvotes: 3

lahsrah
lahsrah

Reputation: 9173

Try: DataFormatString = @"{0:hh\-mm}"

Upvotes: 5

Related Questions