115

I have this attribute in my view model:

[DataType(DataType.DateTime)]
public DateTime? StartDate { get; set; }

If I want to display the date, or populate a textbox with the date, I have these:

<%: Model.StartDate %>

<%: Html.TextBoxFor(m => m.StartDate) %>

Whenever the date is displayed, it's displayed like: 01/01/2011 12:00:00 AM

But I'd like to only display 01/01/2011

Is there a way to apply a display format with data annotations? I don't want to have to go to every instance where I display a date, and add some code to format it.

Steven
  • 18,761
  • 70
  • 194
  • 296

11 Answers11

170

Try tagging it with:

[DisplayFormat(ApplyFormatInEditMode = true, DataFormatString = "{0:MM/dd/yyyy}")]
David Fox
  • 10,603
  • 9
  • 50
  • 80
  • 1
    Just tried that, it still displays the time in the TextBox and when it's just displayed on the page. – Steven Mar 09 '11 at 22:30
  • 3
    @Steven: Can you try removing `[DataType(DataType.DateTime)]` if you haven't already? – David Fox Mar 09 '11 at 22:35
  • 12
    `DisplayFormat` works only with `EditorFor` and `DisplayFor` helpers. – Ε Г И І И О Jun 15 '12 at 09:12
  • 1
    Great answer - I was missing the "ApplyFormatInEditMode" parm. – CodeHxr Jan 07 '13 at 19:49
  • Minor suggestion, change the format string to "{0:d}", that will work with respect to your locale as well. – Yooakim Feb 05 '14 at 17:32
  • hi, a question for you, our website hosted in another country, it needs `MM/dd/yyyy` format else it shows validation error `The field BeginDate must be a date.`, how can i make sever to accept `dd/MM/yyyy` format? – Shaiju T Apr 02 '15 at 06:04
  • 6
    I was able to resolve this by format parameter in Html.TextBoxFor. By setting it to `@Html.TextBoxFor(model => model.YOUR_DATE, "{0:MM/dd/yyyy}")` I was able to get just the date to display. Found this here [http://stackoverflow.com/a/14529347/2938775]. – Caffeinius Apr 28 '15 at 14:51
60

Did you try this?

[DataType(DataType.Date)]
rk1962
  • 1,713
  • 3
  • 24
  • 29
27

In mvc 4 you can easily do it like following using TextBoxFor..

@Html.TextBoxFor(m => m.StartDate, "{0:MM/dd/yyyy}", new { @class = "form-control default-date-picker" })

So, you don't need to use any data annotation in model or view model class

Hasib Tarafder
  • 5,773
  • 3
  • 30
  • 44
  • yup, those DataAnnotations work great for EditorFor helpers, but, when rendering a custom grid with an IEnumerable(Of Entity) you have to use the .TextBoxFor and this was my issue. Thanks – bkwdesign Apr 22 '15 at 18:57
23

If your data field is already a DateTime datatype, you don't need to use [DataType(DataType.Date)] for the annotation; just use:

[DisplayFormat(ApplyFormatInEditMode = true, DataFormatString = "{0:MM/dd/yyyy}")]

on the jQuery, use datepicker for you calendar

    $(document).ready(function () {
        $('#StartDate').datepicker();
    });

on your HTML, use EditorFor helper:

    @Html.EditorFor(model => model.StartDate)
CodeArtist
  • 5,534
  • 8
  • 40
  • 65
Toddt
  • 259
  • 2
  • 4
  • "If your data field is already a DateTime data type, you don't need to use `[DataType(DataType.Date)]`" => that was helpful – MD TAREQ HASSAN Aug 24 '19 at 06:06
  • But for web api, display format is not enforcing `DataFormatString = "{0:MM/dd/yyyy}"` (did not get any 400 even request body had other format i.e. `{"dob":"31/12/1990"}`) – MD TAREQ HASSAN Aug 24 '19 at 06:10
19

Apply DataAnnotation like:

[DisplayFormat(DataFormatString = "{0:MMM dd, yyyy}")]
Chirag
  • 4,046
  • 1
  • 33
  • 24
13

Use this, but it's a complete solution:

[DataType(DataType.Date)]
[DisplayFormat(ApplyFormatInEditMode = true, DataFormatString = "{0:dd/MM/yyyy}")]
Renatto Machado
  • 1,534
  • 2
  • 16
  • 33
7

That works for me

[DataType(DataType.DateTime)]
[DisplayFormat(DataFormatString = "{0:dd-MM-yyyy}", ApplyFormatInEditMode = true)]
Philip Stratford
  • 4,513
  • 4
  • 45
  • 71
FelixAVeras
  • 964
  • 9
  • 17
5

After Commenting

          // [DataType(DataType.DateTime)] 

Use the Data Annotation Attribute:

[DisplayFormat(ApplyFormatInEditMode = true, DataFormatString = "{0:dd/MM/yyyy}")]

STEP-7 of the following link may help you...

http://ilyasmamunbd.blogspot.com/2014/12/jquery-datepicker-in-aspnet-mvc-5.html

Md. Ilyas Hasan Mamun
  • 1,848
  • 2
  • 24
  • 15
4

Use EditorFor rather than TextBoxFor

collusionbdbh
  • 701
  • 1
  • 6
  • 18
2

This is my favourite way to do that with anotations in your model:

[DisplayFormat(DataFormatString = "{0:dd/MM/yyy}")]
Arkox
  • 51
  • 2
0

set your property using below code at the time of model creation i think your problem will be solved..and the time are not appear in database.you dont need to add any annotation.

private DateTime? dob;
        public DateTime? DOB
        {
            get
            {
                if (dob != null)
                {
                    return dob.Value.Date;
                }
                else
                {
                    return null;
                }

            }
            set { dob = value; }
        }