基本上,我想要的是在整个应用程序中格式化DateTime
并在整个Web应用程序中以特定格式显示它。有没有更好的解决方案来写ToString("{0:stringformathere}", obj)
?
答案 0 :(得分:1)
尝试覆盖网页的InitializeCulture方法。
Thread.CurrentThread.CurrentCulture = <some custom culture>
答案 1 :(得分:0)
您可以创建自己的编辑/显示模板,并在整个应用程序中重复使用。
的查看/共享/ 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")