Http://www.tuicool.com/articles/BNVBR3
In SQL Server2005, if a field is defined as datetime, it will be displayed by default in the view of the date and time of the month (e.g. 2013/8/6 13:37:33)
If you only want to display the form of a year and a month, do not time and seconds, then what?
The first method is to set a template for the time display and then call the template where it needs to be displayed.
1. Under the Share folder, create a folder DisplayTemplates
2. Under the DisplayTemplates folder, create a view longdatetime.cshtml
3. Enter the code in the View longdatetime.cshtml
System. DateTime@Model. Tolongdatestring ()
Of course, the following sentence can also be changed to @model.toshortdatestring () or other date format.
4, in the need to display the date of the place, by the original
@Html.displayfor (ModelItem = Item. Posttime)
Replaced by
@Html.displayfor (ModelItem = Item. Posttime,"longdatetime")
This completes the display transformation of the time format. Displayed by the original display (2013/8/6-13:37:33) (August 6, 2013)
The second method: Add DisplayFormat attribute above the model class.
Such as:
[Display (Name = " release Time:")] [DisplayFormat (dataformatstring = "{0:yyyy mm month DD day}")] public virtual System.DateTime posttime { get; Set; }
displayed, this format is still August 6, 2013.
How do @html.displayfor in MVC control the display format of a date (go)