I have a table in my razor code that loops through all of the items in the model and creates a table row for each item. I'm trying to figure out how to format the date to MM/dd/yyyy instead of the default "MM/dd/yyyy HH:mm:ss" format.
<table class="table-condensed">
    <thead>
        <tr>
            <th>Company</th>
            <th>Contract No</th>
            <th>Description</th>
            <th>Expires</th>
        </tr>
    </thead>
    <tbody>
        @foreach (var item in Model)
        {
            <tr>
                <td>@Html.ActionLink(@item.CompanyName, "ViewContract", new { contractID = @item.ID }) </td>
                <td width="200">@item.ContractNumber</td>
                <td>@item.Description</td>
                <td>@item.ExpireDate</td>
            </tr>
            }
    </tbody>    
</table>
I have tried @item.ExpireDate.ToString("MM/dd/yyyy") but it throws an error saying that .ToString does not take an argument.
If you're using c# 6 features you can use a null-conditional.
@(item.ExpireDate?.ToString("MM/dd/yyyy"))
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With