Andrew Kilburn
Andrew Kilburn

Reputation: 2251

How to format a date to remove the time?

Simple question but simple work-arounds don't seem to be working for me. At the moment in my model I have this:

  [DataType(DataType.Date)]
        [DisplayFormat(DataFormatString = "{0:dd-MM-yyyy}", ApplyFormatInEditMode = true)]
        public DateTime? DebitDate { get; set; }

View:

  @foreach (var i in Model.ReportData) {
                                    <tr>
                                        <td>@i.TypeID</td>
                                        <td>@i.CPAttached</td>
                                        <td>@i.ReviewName</td>
                                        <td>@i.ClaimID</td>
                                        <td>@i.Line</td>
                                        <td>@i.AccountNo</td>
                                        <td>@i.SupplierName</td>
                                        <td>@String.Format("{0:C0}", i.Amount)</td>
                                        <td>@i.StatusCategoryDesc</td>
                                        <td>@i.DateSent</td>
                                        <td>@i.DayOS</td>
                                        @if (Model.ReportData.Select(r => r.ClientID).FirstOrDefault() == 1) {
                                            <td>@i.NominalPeriod</td>
                                        }
                                        else {
                                            <td>@i.SLInvoiceNo</td>
                                        }
                                        <td>@i.Area</td>
                                        <td>@i.DebitRef</td>
                                        <td>@i.DebitDate</td>
                                        <td>@i.DeductDate</td> 
                                        <td>@i.APLReason</td>
                                        @if (Model.ReportData.Select(r => r.ClientID).FirstOrDefault() == 1) {
                                            <td>@i.DeptNo</td>
                                            <td>@i.DeptName</td>
                                        }
                                    </tr>

But this doesnt work.

Does anybody have any quick work arounds?

View or model, either will do.

Upvotes: 1

Views: 69

Answers (2)

user586399
user586399

Reputation:

        [DataType(DataType.Date)]
        [DisplayFormat(DataFormatString = "{0:dd-MM-yyyy}", ApplyFormatInEditMode = true)]
        public DateTime? DebitDate { get; set; }

This only works when automatically generating your model @Html.ModelFor, but in your case your are generating it manually using foreach

Upvotes: 0

Rahul Tripathi
Rahul Tripathi

Reputation: 172588

You can try like this:

<td>@i.DebitDate.Value.ToShortDateString()</td>

Upvotes: 2

Related Questions