我想,我的数据库中的DateTimeOffset在View中输出为普通日期(例如 - > 10.12.2011)。但是它显示了这样的日期格式 - > 10.12.2011 12:30:30 +00:00 我只想在视图中更改dateformat。
我的ParticialView看起来像这样:
@foreach (var ShippedData in Model.ShippedData) {
<tr>
<td>@ShippedData.ReceiveDate.ToString()</td>
</tr>
}
我的模特是这样的:
[DisplayFormat(ApplyFormatInEditMode = true, DataFormatString = "{0:yyyy/MM/dd}")]
public DateTimeOffset? ReceiveDate { get; set; }
错误的输出:10.12.2011 12:30:30 +00:00
正确输出应为:10.12.2011
答案 0 :(得分:4)
调用ToString()
只会返回默认格式。您需要使用DisplayFor
才能使用DisplayFormat
属性:
@Html.DisplayFor(m => ShippedData.ReceiveDate)
或者,在ToString
中添加所需的格式:
@ShippedData.ReceiveDate.ToString("yyyy/MM/dd")