DateTimeFor displayed?

Jun 13, 2011 at 1:28 AM

Can we display a DateTime field with the correct local?  The DisplayField outputs a DateTime in raw format as opposed to what was entered via the DateTimeFor.



Jun 13, 2011 at 9:29 AM

try to apply a DataAnnotation that specify the formato you would like to apply to the date property of your ViewModel:

[DisplayFormat(DataFormatString = "{0:G}")]

A G format usually is good however if you don't like it see here for standard format or here:
for custom formats

The DisplayField formats according to the data annotation, if none is provided a "rough" standard format is shown
Jun 13, 2011 at 8:17 PM

Thanks.  I had to change the DisplayField to a DisplayFor though otherwise the format wasn't applied to it.



Jun 14, 2011 at 8:16 AM

You rare right in the actual versuion the DisplayField just work with the FormatAttribute, that is an enhabcement of the DisplayFormat attribute. In the next version that will be available, I hope, tomorrow this limitation has been removed.

However in your case iti s ok to use DisplayFor I suppose, since the DisplayField has the main purpose to allow modification of the field on the client side. In fact it enclose the strin into a span and give it a name that you can compute with the PrefixedId helper. Moreover it recor format information also on the client side so, on the client side you have all you need to modify the filed properly preserving the origimìnal format.