I came across a very interesting problem. If I use ViewData to pass the DateTime value to the view and then display it inside the text field, even if I use String.Format in the same way, 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 displays 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 first line using the Html helper creates the date format in one way, and the second in a very different way. Any ideas why?
Note. I am now in Brazil, so the standard format for short dates here is dd / MM / yyyy.
source
share