Assign format of DateTime with data annotations?

C#asp.netasp.net MvcData Annotations

C# Problem Overview


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.

C# Solutions


Solution 1 - C#

Try tagging it with:

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

Solution 2 - C#

Did you try this?

[DataType(DataType.Date)]

Solution 3 - C#

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

Solution 4 - C#

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)

Solution 5 - C#

Apply DataAnnotation like:

[DisplayFormat(DataFormatString = "{0:MMM dd, yyyy}")]

Solution 6 - C#

Use this, but it's a complete solution:

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

Solution 7 - C#

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

Solution 8 - C#

That works for me

[DataType(DataType.DateTime)]
[DisplayFormat(DataFormatString = "{0:dd-MM-yyyy}", ApplyFormatInEditMode = true)]

Solution 9 - C#

Use EditorFor rather than TextBoxFor

Solution 10 - C#

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

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

Solution 11 - C#

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; }
        }

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionStevenView Question on Stackoverflow
Solution 1 - C#David FoxView Answer on Stackoverflow
Solution 2 - C#rk1962View Answer on Stackoverflow
Solution 3 - C#Hasib TarafderView Answer on Stackoverflow
Solution 4 - C#ToddtView Answer on Stackoverflow
Solution 5 - C#ChiragView Answer on Stackoverflow
Solution 6 - C#Renatto MachadoView Answer on Stackoverflow
Solution 7 - C#Md. Ilyas Hasan MamunView Answer on Stackoverflow
Solution 8 - C#FelixAVerasView Answer on Stackoverflow
Solution 9 - C#collusionbdbhView Answer on Stackoverflow
Solution 10 - C#ArkoxView Answer on Stackoverflow
Solution 11 - C#user4719992View Answer on Stackoverflow