Assign format of DateTime with data annotations?

前端 未结 10 1504
梦如初夏
梦如初夏 2020-11-27 14:03

I have this attribute in my view model:

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

If I want to display the dat

相关标签:
10条回答
  • 2020-11-27 14:08

    Try tagging it with:

    [DisplayFormat(ApplyFormatInEditMode = true, DataFormatString = "{0:MM/dd/yyyy}")]
    
    0 讨论(0)
  • 2020-11-27 14:11

    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; }
            }
    
    0 讨论(0)
  • 2020-11-27 14:12

    Apply DataAnnotation like:

    [DisplayFormat(DataFormatString = "{0:MMM dd, yyyy}")]
    
    0 讨论(0)
  • 2020-11-27 14:15

    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)
    
    0 讨论(0)
  • 2020-11-27 14:15

    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

    0 讨论(0)
  • 2020-11-27 14:22

    Did you try this?

    [DataType(DataType.Date)]
    
    0 讨论(0)
提交回复
热议问题