2017-08-25 72 views
-1

我的日期有问题。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> 

我该怎么办?

感谢您的帮助!

+0

声明的权利,当你尝试不同的失败“解决方案”,这是什么情况? – z3nth10n

+0

您是否尝试过'DataFormatString = @“{0:dd \/MM \/yyyy}”'? –

+0

您需要为您的类声明'使用System.ComponentModel.DataAnnotations;'语句来理解'DisplayFormat'。 – Tiramonium

回答

0

您可以创建适用于datetime和nullable类型的扩展方法。

这可能帮助:

//datetime 
public static string ToShortDateString(this DateTime date){ 
    return date.ToString("MM/dd/yyyy"); 
} 
//nullable datetime 
public static string ToShortDateString(this DateTime? date){ 
    if(date==null){ 
     return null; 
    } 
    return date.Value.ToShortDateString(); 
} 

在你的代码,你可以因为你使用可空类型不能使用格式字符串与toString方法使用这样的

@item.Date_Of_Birth.ToShortDateString(); 
-1

。需要做的事情如

(Date_Of_Birth.HasValue)?Date_Of_Birth.Value.ToString("dd/MM/yyyy"):Date_Of_Birth 

在您的看法。

0

您提供的数据注释不应该存在问题。我其实只是尝试了一个我自己的应用程序,它工作正常。

我在你的问题注意到的是,您发布为我们展示了一个名为Date_Of_Birth财产的观点..但你的数据注解是上面一个叫MyDateTime

财产的唯一的事情是错误的?

[DisplayFormat(DataFormatString = "{0:dd/MM/yyyy}", ApplyFormatInEditMode = true)] 
public Nullable<System.DateTime> Date_Of_Birth{ get; set; } 

让我知道这是否有帮助。

1

首先,大多数时候你不应该做任何事情来获得正确的日期。了解日期始终在内部以相同格式存储,只有在显示日期时才格式化。其次,您应该将您的应用程序全球化设置为使用所需的日期格式,因此您不需要特别设置格式。这是一个web.config设置。

三,显示属性仅在使用编辑器/显示模板时起作用,所以您必须使用@Html.DisplayFor(m => m.MyDate)来获取该属性指定的格式(或EditorFor如果格式化以进行编辑)。

0

我用这两种方式(变通)来格式化MVC中的日期字符串。

  1. 创建HTML辅助扩展方法(类似于超级用户曾提议)

    public static string FormatDate(this HtmlHelper helper, Datetime? date) 
    { 
        //your own method to convert nullable date to short date string 
    
    } 
    

,并在HTML页面中,您包括目录所在的扩展方法,例如:

@using YourApp.HtmlHelpers 

,并使用它:

@Html.FormatDate(Model.Date_Of_Birth); 
  • 创建模型类中的另一属性返回格式的日期字符串(可能是可怕的想法)。而在视图页面,只需要使用这个属性,而不是日期时间属性,例如:

    public string FormattedDate { 
        get { return Date_Of_Birth.Value.ToString("dd/MM/yyyy");} 
    } 
    
  • 0

    的解决方案是建立特殊的方法:

    //nullable datetime 
    public static string ToShortDateString(this DateTime? date){ 
        if(date==null){ 
         return null; 
        } 
        return date.Value.ToShortDateString(); 
    } 
    

    感谢@Super用户,@Amy ,和其他伙计很多非常快速的答案,不同的想法,并分享!

    @Tiramonium你也有过在

    using System.ComponentModel.DataAnnotations;