2014-01-30 23 views
0

所以基本上,我想要的是在整个应用程序中格式DateTime并在整个Web App中以特定格式显示它。有没有更好的解决方案,只写ToString("{0:stringformathere}", obj)asp.net mvc格式整个应用程序的DateTime

+0

可能重复[你怎么全局设置在ASP.NET中的日期格式?] (http://stackoverflow.com/questions/300841/how-do-you-globally-set-the-date-format-in-asp-net) –

回答

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") 
相关问题