MVC check for accepting DateTime in ddMMyyyy format

I just want to accept Date in ddMMyyyy format when sending. But this gives an error, for example,

The FromDate field must be a date.

enter image description here

But, when I set the date in MMddyyyy, it takes pkkttrfg forward.
My model

public class CompareModel { [Required] [DisplayFormat(DataFormatString = "{0:dd-MM-yyyy}", ApplyFormatInEditMode = true)] public DateTime FromDate { get; set; } [Required] [DisplayFormat(DataFormatString = "{0:dd-MM-yyyy}", ApplyFormatInEditMode = true)] public DateTime TODate { get; set; } } 

My view part

 <div class="form-group"> @Html.LabelFor(model => model.FromDate, new { @class = "control-label col-md-2" }) <div class="col-md-10"> @Html.EditorFor(model => model.FromDate) @Html.ValidationMessageFor(model => model.FromDate) </div> </div> <div class="form-group"> @Html.LabelFor(model => model.TODate, new { @class = "control-label col-md-2" }) <div class="col-md-10"> @Html.EditorFor(model => model.TODate) @Html.ValidationMessageFor(model => model.TODate) </div> </div> <div class="form-group"> <div class="col-md-offset-2 col-md-10"> <input type="submit" value="Create" class="btn btn-default" /> </div> </div> 
+6
source share
3 answers

The problem is that it uses javascript to verify this, and you need to rewrite it.

 jQuery(function ($) { $.validator.addMethod('date', function (value, element) { if (this.optional(element)) { return true; } var ok = true; try { $.datepicker.parseDate('dd/mm/yy', value); } catch (err) { ok = false; } return ok; }); }); 

What was taken from this answer

+7
source

Just try adding

  <system.web> <globalization culture="en-GB"/> </System.web> 

His work is for me with the same problem

+5
source

the same question I answer earlier DatePicker format problem

Try setting the culture in the web config corresponding to the date format.

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

See this link. ASP.NET date support for different cultures.

http://maheshde.blogspot.com.au/2011/09/aspnet-date-time-support-for-different.html

+2
source

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


All Articles