【问题标题】:asp.net mvc format DateTime in whole application整个应用程序中的 asp.net mvc 格式 DateTime
【发布时间】:2014-01-30 09:29:35
【问题描述】:

所以基本上,我想要的是在整个应用程序中格式化 DateTime 并在整个 Web 应用程序中以特定格式显示它。有没有更好的办法只写ToString("{0:stringformathere}", obj)

【问题讨论】:

标签: asp.net-mvc datetime-format


【解决方案1】:

尝试覆盖网页的 InitializeCulture 方法。

Thread.CurrentThread.CurrentCulture = <some custom culture>

http://msdn.microsoft.com/en-us/library/bz9tc508.aspx

【讨论】:

    【解决方案2】:

    您可以创建自己的编辑器/显示模板并在整个应用程序中重复使用。

    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")
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2013-09-14
      相关资源
      最近更新 更多