【问题标题】:MVC 5 Format Datetime in Model模型中的 MVC 5 格式日期时间
【发布时间】:2014-08-15 07:37:21
【问题描述】:

我的数据库中有几个 DateTime 字段。通常我刚刚在我的模型中创建了这个:

[Display(Name = "Downtime start")]
public DateTime? Downtime_start { get; set; }

注意:它必须可以为空。

所以这很好用。但问题是它还会到处显示秒数,有点烦人。

所以我不能通过在我的模型中进行一些解析来解决这个问题吗?像这样?

private string Parsed_Downtime_start;

[Display(Name = "Downtime_start")]
public DateTime? Downtime_start
{
    get 
    {
        if (Parsed_Downtime_start != null)
        {
            return DateTime.Parse(Parsed_Downtime_start);
        }
        else 
        {
            return null;
        }

    }
    set 
    {
        if (value != null)
        {
            Parsed_Downtime_start = value.Value.ToString("MM/dd/yyyy hh:mm tt");
        }
        else
        {
            Parsed_Downtime_start = null;
        }

    }
}

这并没有真正改变任何东西。但是,如果我以另一种方式进行操作并将其作为字符串保存到数据库中并将其解析为 DateTime ,则它可以正常工作。但我真的不想将数据库中的日期时间保存为字符串。

【问题讨论】:

  • 您不想更改实际值,只更改演示文稿。 Google 可以告诉您 Html.DisplayFor 使用的 Views / Shared / DisplayTemplates。
  • 你可以在这里找到答案:stackoverflow.com/questions/18288675/…

标签: c# asp.net-mvc datetime


【解决方案1】:

尝试使用

[DisplayFormat(ApplyFormatInEditMode = true, DataFormatString = "{0:MM/dd/yyyy}")]

它适用于 EditorFor 和 DisplayFor 助手

【讨论】:

  • 这很好用,直到您想使用本地化。我想并非所有事情都必须简单......
  • 他想显示小时和分钟,而不是秒。这表明没有时间。
【解决方案2】:

当你使用这个属性时

Convert.ToDateTime(Downtime_start).ToLongDateString()

应该允许使用小时、秒和分钟来反转(保存到 db)。

【讨论】:

    【解决方案3】:

    这个解决方案有点复杂,因为我必须让它与 Javascript Datetimepciker 插件一起工作。 (https://github.com/xdan/datetimepicker) 而且格式必须非常特别。

    所以我最终创建了一个辅助方法。

    namespace MvcHtmlHelpers
    {
        public static class MvcHtmlHelpers
        {
            public static string ToString(this DateTime inputDate, bool FixTime)
            {
                if (FixTime)
                {
                    string returnString;
    
                    if (inputDate == DateTime.MinValue)
                    {
                        returnString = "N/A";
                    }
                    else
                    {
                        returnString = inputDate.ToShortDateString() + ' ' + inputDate.ToShortTimeString();
                    }
                    return returnString;
                }
                return inputDate.ToString();
            }
        }
    }
    

    我的表单有一个“查看阶段”和一个由编辑按钮控制的“编辑模式阶段”。 而且我正在重载 .Value.ToString(true) 方法以使用辅助方法正确格式化日期。

    <section class="col col-4">
        <label class="label">@Html.DisplayNameFor(model => model.Downtime_start)</label>
        <label class="input edit-case">
            <i class="icon-append fa fa-calendar"></i>
            <input type="text" id="Downtime_start" value="@Model.Downtime_start.Value.ToString(true)">
        </label>
        <label class="input state-disabled view-case">
            <i class="icon-append fa fa-calendar"></i>
            <input type="text" id="lbl_Downtime_start" disabled="disabled" value="@Model.Downtime_start.Value.ToString(true)">
        </label>
    </section>
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2014-06-04
      • 1970-01-01
      • 2015-02-20
      • 2014-04-20
      • 1970-01-01
      • 2015-12-17
      相关资源
      最近更新 更多