如何在 Razor 视图中转换日期时间格式? [英] How to convert date time format in a Razor view?

查看:108
本文介绍了如何在 Razor 视图中转换日期时间格式?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我需要在 Razor 视图中以以下格式显示日期时间字符串:

I need to display a date time string in the following format in a Razor view:

2016 年 6 月 24 日

24 Jun'16

如何在 Razor 视图中转换字符串?

how can I convert the string in a Razor view?

推荐答案

就像平常一样:

@Model.YourDateTime.ToString("d MMM \\'yy")

\\ 转义 ',因为它是一个保留字符.剩下的就很简单了.

The \\ escapes the ', since that is a preserved character. The rest is quite straightforward.

另一种选择是在 DisplayFor 中使用显示属性:

Another option is the use of display attributes with DisplayFor:

[DisplayFormat(DataFormatString = "{0:d MMM \\'yy}")]
public DateTime YourDateTime { get; set }

这篇关于如何在 Razor 视图中转换日期时间格式?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆