Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Show DateTimeOffset in View as normal Date Format

I want, that my DateTimeOffset in the database outputs in the View as a normal date (for example -> 10.12.2011). But it shows the date format like this -> 10.12.2011 12:30:30 +00:00. I only want to change the dateformat in the view.

My ParticialView looks like this:

@foreach (var ShippedData in Model.ShippedData) {
    <tr>
      <td>@ShippedData.ReceiveDate.ToString()</td>
    </tr>
}

My Model like this:

[DisplayFormat(ApplyFormatInEditMode = true, DataFormatString = "{0:yyyy/MM/dd}")]
public DateTimeOffset? ReceiveDate { get; set; }

Wrong Output: 10.12.2011 12:30:30 +00:00

Correct Output should be: 10.12.2011

like image 621
NinjaCoder Avatar asked Sep 04 '26 09:09

NinjaCoder


1 Answers

Calling ToString() just returns the default formatting. You either need to use DisplayFor so that the DisplayFormat attribute will be utilized:

@Html.DisplayFor(m => ShippedData.ReceiveDate)

Or, include your desired formatting in ToString:

@ShippedData.ReceiveDate.ToString("yyyy/MM/dd")
like image 70
Chris Pratt Avatar answered Sep 06 '26 22:09

Chris Pratt



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!