加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 编程开发 > asp.Net > 正文

asp.net-mvc – 格式化日期只有月份和年份 – “MMM-yyyy”

发布时间:2020-12-16 07:13:11 所属栏目:asp.Net 来源:网络整理
导读:我正在研究MVC 4.0./ C#/Razor视图.在模型中我有一个约会 [Display(Name = "For Period")][DisplayFormat(DataFormatString = "{0:dd-MMM-yyyy}")] public System.DateTime ForPeriod { get; set; } 在视图中我正在使用日期 @Html.DisplayFor(modelItem = it
我正在研究MVC 4.0./ C#/Razor视图.在模型中我有一个约会

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

在视图中我正在使用日期

@Html.DisplayFor(modelItem => item.ForPeriod)

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

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

解决方法

您可以按照以下方式在View文件中格式化日期

<span>@Convert.ToDateTime(item.EffectiveDate).ToString("MMM-yyyy")</span>

也可以从模型中格式化日期

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

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读