我正在研究 MVC 4.0./C#/Razor View 。在模型中我有一个约会
[Display(Name = "For Period")]
[DisplayFormat(DataFormatString = "{0:dd-MMM-yyyy}")]
public System.DateTime ForPeriod { get; set; }
在 View 中我正在使用
@Html.DisplayFor(modelItem => item.ForPeriod)
结果我得到了“12-May-2015”格式的日期。我想从这个日期跳过这一天,结果我想要“2015 年 5 月”。
我的问题是我应该在 View 中使用什么来获得这种“MMM-yyyy”格式?
最佳答案
[Display(Name = "For Period")]
[DisplayFormat(DataFormatString = "{0:MMM-yyyy}")]
public System.DateTime ForPeriod { get; set; }
有关 C# 格式日期的更多信息可以在 on MSDN here. 中找到。
还有一个使用自定义编辑器模板的附加选项,您可以阅读有关堆栈溢出的这篇文章以了解更多详细信息 - ASP.NET MVC 4 Editor Template for basic types
关于asp.net-mvc - 将日期格式化为仅月和年 - "MMM-yyyy",我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/30229790/