I have this attribute in my view model:
[DataType(DataType.DateTime)]
public DateTime? StartDate { get; set; }
If I want to display the dat
Try tagging it with:
[DisplayFormat(ApplyFormatInEditMode = true, DataFormatString = "{0:MM/dd/yyyy}")]
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; }
}
Apply DataAnnotation like:
[DisplayFormat(DataFormatString = "{0:MMM dd, yyyy}")]
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)
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
Did you try this?
[DataType(DataType.Date)]