JQuery Datepicker Passing 01/01/0001 12:00:00 AM

asp.net-mvcjqueryjquery-uijquery-ui-datepickernet

I have a DateOfBirth field in a view model like this,

[Required]
[DataType(DataType.Date)]
[Display(Name = "Date Of Birth")]
public DateTime DateOfBirth { get; set; }

and the view that has this,

<li>
    @Html.LabelFor(m => m.DateOfBirth)
    @Html.TextBoxFor(m => m.DateOfBirth)
</li>

and some jQuery to add the datepicker,

<script type="text/javascript">
    $('#DateOfBirth').datepicker({
        yearRange: '1906:1994',
        changeMonth: true,
        changeYear: true,
        showAnim: 'bounce'
    });
</script>

If I choose a date such as 07/19/1981, July 19, 1981 it passes the value into the controller as 01/01/0001 12:00:00 AM and I get the error "The value '07/19/1981' is not valid for Date Of Birth." I've noticed that if the value for the day is 12 or less it passes it goes through but if the day is above 12 it will fail. So I tried changing the format in the datepicker to

dateFormat: 'dd/mm/yy'

and then when I submit the form it gives the error "The field Date Of Birth must be a date."

What do I need to do to fix this? I'd like to be able to display the date as month/day/year if at all possible.

Best Answer

Try using datetime picker like this :

$("#StartDate").datetimepicker({ timeFormat: 'hh:mm:ss', dateFormat: 'dd.mm.yy' });

You can include timepicker from this link. I think the format you provided is wrong check MSDN for correct datetime format string, but in my case worked. You can also add correct format to your datepicker.