我想将日期编辑为"23/5/2017"
,将时间编辑为"03:53 PM"
。我的模型的日期DisplayFormat
为"{0:dd/M/yyyy}"
,时间DisplayFormat
为"{0:hh:mm tt}"
[DataType(DataType.DateTime)]
[Display(Name = "Date")]
[DisplayFormat(DataFormatString = "{0:dd/M/yyyy}", ApplyFormatInEditMode = true)]
public DateTime LogInDate { get; set; }
[DataType(DataType.DateTime)]
[Display(Name = "Time In")]
[DisplayFormat(DataFormatString = "{0:hh:mm tt}", ApplyFormatInEditMode = true)]
public DateTime LogInTime { get; set; }
查看代码:
<div class="form-group">
@Html.LabelFor(model => model.LogInDate, htmlAttributes: new { @class = "control-label col-md-2" })
<div class="col-md-10">
@Html.EditorFor(model => model.LogInDate, new { htmlAttributes = new { @class = "form-control" } })
@Html.ValidationMessageFor(model => model.LogInDate, "", new { @class = "text-danger" })
</div>
</div>
<div class="form-group">
@Html.LabelFor(model => model.LogInTime, htmlAttributes: new { @class = "control-label col-md-2" })
<div class="col-md-10">
@Html.EditorFor(model => model.LogInTime, new { htmlAttributes = new { @class = "form-control" } })
@Html.ValidationMessageFor(model => model.LogInTime, "", new { @class = "text-danger" })
</div>
</div>
以下是日期的HTML:
<input class="form-control text-box single-line input-validation-error" data-val="true" data-val-date="The field Date must be a date." data-val-required="The Date field is required." id="LogInDate" name="LogInDate" type="datetime" value="">
和时间的HTML:
<input class="form-control text-box single-line input-validation-error" data-val="true" data-val-date="The field Time In must be a date." data-val-required="The Time In field is required." id="LogInTime" name="LogInTime" type="datetime" value="">
索引页面工作正常:
[]
此外,如果我可以对此进行排序,是否可以使用DataFormatString
中的任何格式说明符(来自Microsoft的“自定义日期和时间格式字符串”页面)来显示/编辑我需要的日期/时间?
非常感谢你帮助我。