Zet
Zet

Reputation: 571

how to remove date from DateTime C#

I need just the time part of DateTime - HH:mm. How can I remove the rest? Can I do it in razor View:

 @Html.DisplayFor(m=>m.model.MondayStart)

or do I have to add some kind of attribute in the model?

 public DateTime MondayStart { get; set; }

Upvotes: 0

Views: 789

Answers (2)

Sirwan Afifi
Sirwan Afifi

Reputation: 10824

You can use DisplayFormat for your property:

[DisplayFormat(DataFormatString = "{HH:mm}")]
public DateTime MondayStart { get; set; }

Or you can create a DisplayTemplate:

@model DateTime?
if (Model.HasValue)
{
    @Model.ToString("HH:mm")
}

Upvotes: 0

user3559349
user3559349

Reputation:

Add a DisplayFormatAttribute to your property and specify the DataFormatString property.

[DisplayFormat(DataFormatString = "{0:HH:mm}")]
public DateTime MondayStart { get; set; }

Upvotes: 3

Related Questions