Search code examples
asp.net-mvc-4datetimerazorhtml-helper

How to display the number of days between two dates in view from within razor


I'm trying to show a drop off date for documents but want to calculate this number from within the view instead of the controller. Here's my code:

@foreach (var item in Model.queue_items)
        {
            <tr class="row">
                <td>@Html.Label("", (string)item.report_name)</td>
                <td>@Html.Label("", DateTime.Now.Subtract(item.expires_date).Days.ToString())</td>
                <td>@Html.Label("", (string)item.state)</td>
                <td><a href="@Url.Action("ReportProcessQueue", "Reporting", new { ReportId = item.report_id })" alt="A Link"><span class="glyphicon glyphicon-folder-open" id="btnOpen" data-edit-id="OpenDocument" title="@Text.Get(Text.eTextType.Button, "Open")"></span></a></td>
            </tr>
        }

This is giving me the number "1408". Not sure what I'm doing wrong. Any ideas?


Solution

  • my original code is actually correct, I wasn't looking at the date the document was created and it was created 1408 days ago

    @foreach (var item in Model.queue_items)
        {
            <tr class="row">
                <td>@Html.Label("", (string)item.report_name)</td>
                <td>@Html.Label("", DateTime.Now.Subtract(item.expires_date).Days.ToString())</td>
                <td>@Html.Label("", (string)item.state)</td>
                <td><a href="@Url.Action("ReportProcessQueue", "Reporting", new { ReportId = item.report_id })" alt="A Link"><span class="glyphicon glyphicon-folder-open" id="btnOpen" data-edit-id="OpenDocument" title="@Text.Get(Text.eTextType.Button, "Open")"></span></a></td>
            </tr>
        }
    

    Hope this helps somebody.