ASP.NET MVC - DisplayFormat attribute

送分小仙女□ 提交于 2019-12-08 11:02:06

问题


In my model i've the MyDate property that has datetime type. I sign the property with the DisplayFormat attribute in this mode:

[DisplayFormat(ApplyFormatInEditMode = true, DataFormatString = "{0:dd/MM/yyyy hh:mm}")]
public DateTime MyDate { get; set; }

in my view:

...
<%= Html.EditorFor(model => model.Evento.MyDate)%>
...

why if the value of property is '2011-05-03 14:47', in my view (into EditorFor) i see '03/05/2011 02.47' ?

The DataFormatString is correct!

thanks so much for reply

Alberto


回答1:


Unless I'm mistaken, the format string of {0:dd/MM/yyyy hh:mm} will output as 03/05/2011 02.47. You're seeing what I would expect to see.

UPDATE: To get the 24 hour notation you can use {0:dd/MM/yyyy HH:mm} with the uppercase HH to designate the hour.




回答2:


That's because : is a reserved character indicating the time separator for the given culture which in your case might happen to be the . character. You want:

DataFormatString = @"{0:dd/MM/yyyy HH\:mm}"

You probably might also want to use HH which is the 24 hour format instead of hh.



来源:https://stackoverflow.com/questions/5903945/asp-net-mvc-displayformat-attribute

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!