2

I'm working on an application in which I retrieve data from a database to be displayed in a table in my cshtml. One of the fields I'm calling is a date field. The data for it in the database is stored in the following format: MMDDYY i.e; 091504. For reasons I need not get into, the data has to be stored in that format in the database. I'm trying to figure out how I can modify its display to show 09/15/04 instead of 091504.

@model IEnumerable<BillingApp.Models.SERV_CHARGE_INT_RATE_NSF_PENALTY>

<table id="Test" class="tablesorter">
<thead>
<tr>
<th>Effective Date</th>
</tr>
</thead>


@if (Model.Count() == 0)
{
    <tbody>
    <tr>
        <td colspan="4">
            No Records match search criteria
        </td>
    </tr>
        </tbody>  
}  
else{
    <tbody>
    @foreach (var item in Model)
    {         
        <tr>
        <td>
            @Html.DisplayFor(modelItem => item.EFFECTIVE_DATE)
        </td>
        </tr>
    }
    </tbody>
    </table>
1
  • Is your date a string or an actual DateTime? Commented Aug 4, 2014 at 16:50

2 Answers 2

2

If you use a DateTime object, you can try using [DisplayFormat] in your model.

Ex :

 [DisplayFormat(DataFormatString = "{MM/dd/yy}")]
 public DateTime EFFECTIVE_DATE { get; set }

If you are using a string instead of DateTime, you could consider converting the data type by using @ByteBlast's solution :

DateTime.ParseExact(item.EFFECTIVE_DATE, "MMddyy", CultureInfo.CurrentCulture);
Sign up to request clarification or add additional context in comments.

5 Comments

Pretty sure he doesn't have an actual DateTime object. Otherwise, yes.
@dodexahedron If you are right, ignore my solution. But he said "is a date field".. !
This is a good solution, but it would be good to explain how to convert his format into a DateTime object.
Like this: var date = DateTime.ParseExact(item.EFFECTIVE_DATE, "MMddyy", CultureInfo.CurrentCulture);
@RomanMik Yes, if he does use a string instead of DateTime, which we don't know yet.
0

There are many ways to handle this.

One would be to have your model class parse the dates coming out of the database into actual DateTime objects and then use those throughout your application (probably the best choice, if you need to do anything else with the dates).

Another would be to slice up the string when displaying it, if all you need to do is display the date. Something along the lines of this:

@foreach (var item in Model)
{         
    <tr>
    <td>
        @(item.EFFECTIVE_DATE.Substring(0,2))/@(item.EFFECTIVE_DATE.Substring(2,2))/@(item.EFFECTIVE_DATE.Substring(4,2))
    </td>
    </tr>
}

1 Comment

If this solves the problem, please mark the question as answered so that it doesn't show up as an unanswered question on the front page any more. Thanks and happy coding!

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.