格式日期只有月份和年份 - "MMM-yyyy"

Irf*_*med 5 asp.net-mvc datetime

我正在研究MVC 4.0./C#/Razor视图.在模型中我有一个约会

[Display(Name = "For Period")]
[DisplayFormat(DataFormatString = "{0:dd-MMM-yyyy}")] 
public System.DateTime ForPeriod { get; set; }
Run Code Online (Sandbox Code Playgroud)

在视图中我正在使用日期

@Html.DisplayFor(modelItem => item.ForPeriod)
Run Code Online (Sandbox Code Playgroud)

结果我得到了"2015年5月12日"格式的约会.我想从这个日期跳过这一天,结果我想要"2015年5月".

我的问题是我应该在View中使用什么来获得这种"MMM-yyyy"格式?

Vul*_*ary 5

[Display(Name = "For Period")]
[DisplayFormat(DataFormatString = "{0:MMM-yyyy}")] 
public System.DateTime ForPeriod { get; set; }
Run Code Online (Sandbox Code Playgroud)

可以在 MSDN 此处找到有关C# 格式日期的更多信息

还有一个使用自定义编辑器模板的附加选项,您可以阅读有关堆栈溢出的这篇文章了解更多详细信息 -用于基本类型的 ASP.NET MVC 4 编辑器模板


小智 4

您可以通过以下方式格式化查看文件中的日期

<span>@Convert.ToDateTime(item.EffectiveDate).ToString("MMM-yyyy")</span>
Run Code Online (Sandbox Code Playgroud)

还可以格式化模型中的日期

[Display(Name = "For Period")]
[DisplayFormat(DataFormatString = "{0:MMM-yyyy}")] 
public System.DateTime ForPeriod { get; set; }
Run Code Online (Sandbox Code Playgroud)