Unable to set datetime format in MVC 4 using data annotations

前端 未结 3 1676
暖寄归人
暖寄归人 2020-12-14 09:41

I will try everything but not working this(dd/MM/yyyy) date format, this always gate mm/dd/yyyy

[Display(Name = \"Release D         


        
相关标签:
3条回答
  • 2020-12-14 10:09

    If you use data-annotation. when display it good. but when edit mode. it still use server format (culture setting) DisplayFormat: like it name, only for display

    There are some option to solve. - paste to server as string with your favorite format - write customer bindding - Setting globalization in web.config for Format : dd/MM/yyy i using setting bellow

    <system.web>
        <globalization culture="en-GB"/>
    ....
    
    0 讨论(0)
  • 2020-12-14 10:19

    You missed 2 things here:

    DataFormatString="{0:dd/MM/yyyy}"; //It is dd/MM/yyyy instead of dd/mm/yyyy
    

    and you also need:

    ApplyFormatInEditMode=true
    

    Try to use this:

    [Display(Name = "Release Date")]
    [DataType(DataType.Date), DisplayFormat( DataFormatString="{0:dd/MM/yyyy}", ApplyFormatInEditMode=true )]
    public Nullable<DateTime> release_date { get; set; }
    
    0 讨论(0)
  • 2020-12-14 10:29

    {0:dd/mm/yyyy} should be {0:dd/MM/yyyy} because mm means minutes, not months:

    [Display(Name = "Release Date")]
    [DataType(DataType.DateTime)]
    [DisplayFormat(ApplyFormatInEditMode = true, DataFormatString = "{0:dd/MM/yyyy}")]
    public Nullable<DateTime> release_date { get; set; }
    
    0 讨论(0)
提交回复
热议问题