Invalid date format

I have a problem checking the date. In my view, I have a jQuery datepicker - I changed the format from yy/mm/ddto mm/dd/yyand now I get client side validation errors. For example,

    The value '02/25/2014' is not valid for Date of Birth.

Javascript:

$('#DateOfBirth').datepicker({
    changeMonth: true,
    changeYear: true,
    dateFormat: "mm/dd/yy",
    yearRange: "-90:-5"
});

View Model:

    [Required]
    [Display(Name = "Date of Birth")]
    public DateTime? DateOfBirth { get; set; }

View:

@Html.TextBoxFor(m=> m.DateOfBirth, "{0:MM/dd/yyyy}", new { @class = "datepicker" })

Any ideas on this?

Thanks.

UPDATE

I forgot something. Validation is not actually performed on the server side. So this has nothing to do with jQuery. ModelState.IsValid == falsefor me.

+4
source share
1 answer

I found a solution here: ASP.NET MVC3 is a DateTime format and this is related to globalization.

My locale en-CAand

System.Globalization.CultureInfo.CurrentCulture.DateTimeFormat.ShortDatePattern

"dd/MM/yyyy".

, Web.config <system.web>

<globalization uiCulture="en-US" culture="en-US"/>

, DateTime .

P.S.

, , ISO 8601 - yyyy-MM-dd ( yyyy/MM/dd, ).

+1

Source: https://habr.com/ru/post/1525306/


All Articles