5

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.

3
  • Is ExpireDate a nullable DateTime? Commented Jan 3, 2017 at 22:45
  • Yes. Additionally I'm using the MVCPagedList NuGet package which requires your model to be passed as a PagedList which is why I'm unable to use an HTML helper Commented Jan 3, 2017 at 23:00
  • hope the link:stackoverflow.com/questions/4679352/… helps Commented Jan 4, 2017 at 8:08

3 Answers 3

10

If you're using c# 6 features you can use a null-conditional.

@(item.ExpireDate?.ToString("MM/dd/yyyy"))

Sign up to request clarification or add additional context in comments.

Comments

4

I am using the PagedList NuGet package which makes the Html helper calls a little different. I was able to format the date by specifying the date format in the data model constructor and then using the display helper.

In the data model:

[DisplayFormat(DataFormatString = "{0: MM/dd/yyyy}")] 
public Nullable<System.DateTime> ExpireDate { get; set; }

In the razor:

<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>@Html.DisplayFor(modelItem => item.ExpireDate)<td>
            </tr>
            }
    </tbody>     </table>

Comments

0

You can also now use

@item.ExpireDate?.ToShortDateString() which converts a DateTime value to the "M/d/yyyy" date format

2 Comments

- error: Operator '?' cannot be applied to operand of type 'DateTime' ?
C# 6 feature ? @cdonner

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.