【问题标题】:How to store only hh:mm in the datetime field?如何在日期时间字段中仅存储 hh:mm?
【发布时间】:2014-03-20 09:46:25
【问题描述】:

我正在使用带有数据注释属性的 codefirst。模型看起来像这样:

    public class Incident
{
    [Key]
    public int IncidentId { get; set; }

    [Display(Name = "Date")]
    [DisplayFormat(ApplyFormatInEditMode = true, DataFormatString = "{0:hh:mm}")]
    public DateTime Date { get; set; }
}

创建视图:

    <div class="editor-label">
        @Html.LabelFor(model => model.Date)
    </div>
    <div class="editor-field">
        @Html.EditorFor(model => model.Date, "time")
        @Html.ValidationMessageFor(model => model.Date)
    </div>

当我尝试创建新模型时,我会在此字段附近收到验证消息。我什至尝试从 create 方法将当前日期传递给视图,但这没有帮助。 有没有办法只存储 hh:mm(没有 dd/MM/yyyy)?

【问题讨论】:

    标签: asp.net asp.net-mvc date ef-code-first data-annotations


    【解决方案1】:

    如果您真的只对一天中的时间感兴趣,那么也许使用TimeSpan 而不是DateTime

    【讨论】:

      【解决方案2】:

      使用时间跨度。

      祝你有美好的一天,

      阿尔伯托

      【讨论】:

      • +1,因为您在我提交答案时一定是在输入这个:)
      • 哈哈哈我刚刚注意到你确实回答了......而且在我之前!干得好:)
      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2018-03-29
      • 1970-01-01
      • 1970-01-01
      • 2019-08-18
      相关资源
      最近更新 更多