c# - 如何格式化 DateTime 以在 Razor 生成的 View 中仅显示为一天中的时间?

标签 c# asp.net-mvc asp.net-mvc-3 datetime razor

我在以下代码中提供位于 model.StartDate 的完整日期时间:

@Html.DisplayFor(model => model.StartDate.ToString("hh:mm tt"),
CultureInfo.CreateSpecificCulture("hu-HU"));

我的目标是获取完整的 DateTime

8/30/2012 11:33:48 AM

仅显示

11:33:48 AM

但是,我收到以下错误:

Templates can be used only with field access, property access,
single-dimension array index, or single-parameter custom indexer expressions.

为什么会出现这个错误?


注意:我引用了此文档来为我的日期时间格式化我的 .ToString:http://msdn.microsoft.com/en-us/library/8kb3ddd4.aspx

最佳答案

您可以在模型类上放置一个 DataAnnotation。像这样的东西 -> [DisplayFormat(DataFormatString = "{0:d}", ApplyFormatInEditMode = true)].

关于c# - 如何格式化 DateTime 以在 Razor 生成的 View 中仅显示为一天中的时间?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/12201986/

相关文章:

c# - 如何在 .NET 中解析 RFC822 日期?

c# - MVC C# - 带条件的数据行中的数据表

c# - 将用户控件添加到 aspx 页面

c# - ASP.NET Core 6 : Users are logged out after 30 minutes, 需要 6 小时

c# - SQL Server 故障转移期间的暂时性错误

javascript - "Content-Disposition", "inline;filename="+ fileName 不强制显示 pdf 文件

c# - 每个文件在 ASP.NET MVC 站点上的初始加载都很慢

c# - 将 Select Option 的选定值作为 href 中的参数传递

c# - MVC4 Razor View header 中@model 和@inherit 的区别?

asp.net-mvc-3 - Telerik MVC 网格在运行时来自集合或字典的动态列