我的日期有问题。C#MVC如何更改模型中的日期格式?
public Nullable<System.DateTime> Date_Of_Birth {get; set;}
我想将当前格式yyyy/MM/dd 00:00:00更改为dd/MM/yyyy。
我试着像解决方案:
1)
[DisplayFormat(DataFormatString = "{0:dd/MM/yyyy}",
ApplyFormatInEditMode = true)]
public DateTime MyDateTime { get; set; }
//但我的MVC项目甚至没有看到 'DisplayFormat'。
2)
在我的情况的ToString( “DD/MM/YYYY”)也不起作用。 例子:@ item.Date_Of_Birth.ToString( “DD/MM/YYYY”)
笔者认为:
@model IEnumerable<School_Project.Models.Data>
<div id="container">
@if (Model != null) {
<div id="table" style="background-color: white; color:black; margin-left:5%;
margin-top:5%; width:35%; border:solid 5px;">
<table class="test" cellpadding="10" style="align-items:center; text-align:center; width:100%; border-collapse: collapse; font-size:18px;">
@foreach(var item in Model)
{
<tr>
<th style="font-size:20px; text-align:left;padding:10px;">Personal Data</th>
</tr>
<tr >
<td>
<b>Name: </b>
</td>
<td>@item.Name</td>
</tr>
<tr>
<td>
<b>Date of Birth: </b>
</td>
<td>@item.Date_Of_Birth</td>
</tr>
我该怎么办?
感谢您的帮助!
声明的权利,当你尝试不同的失败“解决方案”,这是什么情况? – z3nth10n
您是否尝试过'DataFormatString = @“{0:dd \/MM \/yyyy}”'? –
您需要为您的类声明'使用System.ComponentModel.DataAnnotations;'语句来理解'DisplayFormat'。 – Tiramonium