Why Date Format in ASP NET MVC Differs when used inside Html Helper?

Posted by Marcio Gabe on Stack Overflow See other posts from Stack Overflow or by Marcio Gabe
Published on 2010-06-18T04:58:52Z Indexed on 2010/06/18 5:03 UTC
Read the original article Hit count: 291

Hi,

I just came across a very interesting issue. If I use ViewData to pass a DateTime value to the view and then display it inside a textbox, even though I'm using String.Format in the exact same manner, I get different formatting results when using the Html.TextBox helper.

<%= Html.TextBox("datefilter", String.Format("{0:d}", ViewData["datefilter"]))%>
<input id="test" name="test" type="text" value="<%: String.Format("{0:d}", ViewData["datefilter"]) %>" />

The above code renders the following html:

<input id="datefilter" name="datefilter" type="text" value="2010-06-18" />
<input id="test" name="test" type="text" value="18/06/2010" />

Notice how the fist line that uses the Html helper produces the date format in one way while the second one produces a very different output. Any ideas why?

Note: I'm currently in Brazil, so the standard short date format here is dd/MM/yyyy.

© Stack Overflow or respective owner

Related posts about asp.net-mvc

Related posts about datetime-format