Why is DisplayFormat DataFormatString not working?

79,316

Solution 1

Why are you using ApplyFormatInEditMode if you have set [Editable(false)]? All ApplyFormatInEditMode does is format the date if you have it in a text box or something for editing, which you probably won't because of the aforementioned.

I was able to get the date to display correctly using the following:

[DisplayFormat(DataFormatString = "{0:yyyy/MM/dd}")]
public DateTime Date 
{
    get
    {
        return DateTime.Now;
    }
    set
    {
        Date = DateTime.Now;
    }
}

and in the view (with resulting output):

@Html.DisplayFor(x => x.Date)        // 2013/05/23
<br />
@Model.Date                          // 23/05/2013 09:57:56
<br />
@Html.DisplayFor(x => Model.Date)    // 2013/05/23

Hope this helps.

Solution 2

If you can't get it working on the model, you could try it on the view.

@Html.TextBoxFor(m => m.ValidFrom, "{0:dd/MM/yyyy}", new {maxlength = 10})

Solution 3

Since You want to exclude the time to get Only Date: At Model:-

 [DisplayFormat(ApplyFormatInEditMode = true, DataFormatString = "{0:yyyy/MM/dd}"]
   public DateTime TheDate { get; set; }

At Views:-

    @Html.DisplayFor(model=> model.TheDate)
    @Html.JQueryUI().DatepickerFor(model => model.TheDate)

The tutorial of the following link may help you.It works for me.

http://ilyasmamunbd.blogspot.com/2014/02/jquery-ui-datepicker-popup-calendar.html

Solution 4

You have to annotate the type as Date

[DataType(DataType.Date)]

Solution 5

In my case a teammate had defined a global display template (aka ~\Views\Shared\DisplayTemplates\DateTime.cshtml) that I didn't realize took precedence over my [DisplayFormat(DataFormatString = "{0:yyyy-MM-dd}")] on my model property.

The solution was to move my format string to new shared template, so the attribute changes to:

// see ~\Views\Shared\DisplayTemplates\DateMyFormat.cshtml for formatting
[UIHint("DateMyFormat")]
public DateTime MovementDate { get; set; }
Share:
79,316
ProfK
Author by

ProfK

I am a software developer in Johannesburg, South Africa. I specialise in C# and ASP.NET, with SQL Server. I have, in some way or another, been involved in software development for about eighteen years, but always learning something new. At the moment that is WPF and MVVM.

Updated on September 10, 2020

Comments

  • ProfK
    ProfK over 3 years

    I have a property in my view model as follows:

    [Editable(false)]
    [Display(Name = "Date")]
    [DisplayFormat(DataFormatString = "{0:yyyy/MM/dd}", ApplyFormatInEditMode = true)]
    public DateTime MovementDate { get; set; }
    

    Yet the markup

    <td>
        @Html.DisplayFor(modelItem => item.MovementDate)
    </td>
    

    renders the date value as 2013/05/15 12:00:00 AM.

    What am I doing wrong? My model:

    public class WithDateModel
    {
        [DisplayFormat(DataFormatString = "{0:yyyy/MM/dd}")]
        public DateTime TheDate { get; set; }
        public WithDateModel()
        {
            TheDate = DateTime.Now;
        }
    }
    

    My view:

    @model ParkPay.WebTests.Models.WithDateModel
    @Html.DisplayFor(m => m.TheDate)
    @section Scripts {
        @Scripts.Render("~/bundles/jqueryval")
    }
    

    What gets rendered:

    2013/05/25 02:23:37 AM
    
  • ProfK
    ProfK almost 11 years
    It is much easier to have a superfluous ApplyFormatInEditMode on each DisplayFormat attribute than to have to add or remove it if I add or remove the [Editable(false)] attribute.
  • Frederik
    Frederik about 10 years
    This solution worked for me. Also, as a side-note, DisplayFormat is ignored in "TextBoxFor".
  • Adam Tolley
    Adam Tolley over 8 years
    DisplayFormat is ignored in "TextBoxFor" - Gotta love microsoft's consistency here :(
  • Filip
    Filip over 8 years
    @AdamTolley agree, completely anti-intuitive
  • Kai Hartmann
    Kai Hartmann almost 8 years
    Setting ApplyFormatInEditMode did it for me.
  • Scott Fraley
    Scott Fraley over 7 years
    Wouldn't this essentially be considered 'hard-coding it' though? I'm using EditorFor() and it's not working either; and I'd REALLY rather not have to 'hard-code' it.
  • brichins
    brichins about 6 years
    This annotation creates an HTML5 date input rather than a textbox, which may or may not be the desired result.
  • GidiBloke
    GidiBloke over 4 years
    Apparently, the attribute works well for @Html.EditorFor and not the other helpers. You can use the EditorFor helper and you can always add your custom class using @Html.EditorFor(x=> x.MyProperty, new { htmlAttributes = new { @class = "MyCssClass" } })