Sreedhar
Sreedhar

Reputation: 30025

Format Time to HH:mm C# + MVC 2

I want 'Time' to be displayed as HH:mm on UI (editable mode)

my code so far :

    [DisplayName("JobProcessEndTime")]
    [Required(ErrorMessage = "Is required field. Format HH:MM (24 hour time)")]
    [DisplayFormat(ApplyFormatInEditMode = true, DataFormatString = "{0:HH:mm}")]
    public TimeSpan JobProcessEndTime { get; set; }

in view

 <%-- <td class="data"><%=Html.TextBoxFor(model => model.JobProcessEndTime)%></td>--%>
            <td class="data"><%= Html.TextBox("JobProcessEndTime", string.Format("{0:HH:mm}", Model.JobProcessEndTime))%></td>

tried both ways in view. still it displays the 'seconds' part.

Upvotes: 2

Views: 7404

Answers (3)

Jason Goemaat
Jason Goemaat

Reputation: 29214

To het this to work in MVC 3 I had to use Html.EditorFor instead of Html.TextBoxFor.

<td class="data">
    <%= Html.EditorFor(x => x.JobProcessEndTime) %>
</td>

Upvotes: 0

Darin Dimitrov
Darin Dimitrov

Reputation: 1038720

<td class="data">
    <%= Html.EditorFor(x => x.JobProcessEndTime) %>
</td>

Upvotes: 0

amit_g
amit_g

Reputation: 31250

The format string should be

@"{0:hh\:mm}"

and either one of the method would work.

Upvotes: 1

Related Questions