【发布时间】:2014-01-30 09:29:35
【问题描述】:
所以基本上,我想要的是在整个应用程序中格式化 DateTime 并在整个 Web 应用程序中以特定格式显示它。有没有更好的办法只写ToString("{0:stringformathere}", obj)?
【问题讨论】:
标签: asp.net-mvc datetime-format
所以基本上,我想要的是在整个应用程序中格式化 DateTime 并在整个 Web 应用程序中以特定格式显示它。有没有更好的办法只写ToString("{0:stringformathere}", obj)?
【问题讨论】:
标签: asp.net-mvc datetime-format
尝试覆盖网页的 InitializeCulture 方法。
Thread.CurrentThread.CurrentCulture = <some custom culture>
【讨论】:
您可以创建自己的编辑器/显示模板并在整个应用程序中重复使用。
Views/Shared/EditorTemplates/CustomDateTime.cshtml
@model DateTime?
@{
String modelValue = "";
var dateFormatToDisplay =
System.Threading.Thread.CurrentThread.CurrentCulture.DateTimeFormat.ShortDatePattern;
if (Model.HasValue)
{
if (Model.Value != DateTime.MinValue)
{
modelValue = Model.Value.ToString(dateFormatToDisplay);
}
}
}
@Html.TextBox("", modelValue)
在视图中
@Html.EditorFor(m => m.DateOfBirth, "CustomDateTime")
【讨论】: