Publish Valid DateTimes from Anywhere

I want to create a simple ASP.Net form that will receive birth dates. I have a model with this property:

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

and, in my opinion (uses datepicker):

 @Html.LabelFor(model => model.BirthDate) @Html.TextBoxFor(model => model.BirthDate, new { @class = "form-control date-picker" }) @Html.ValidationMessageFor(model => model.BirthDate, "", new { @class = "text-danger" }) 

The only thing I installed on my javascript datepicker:

 format: 'L' 

because I want to show dates without times.

Now I can not save due to a validation error:

The value '12/23/2000' is not valid for BirthDate.

Now I KNOW his globalization error ... I need to set the culture in the <globalization> element in web.config, etc. But did you notice that I did not specify formats (except for the datepicker "L" format)? I want this form to give birth to people in any country, do not have a specific culture / format, etc.

Is it possible? How can i do this?

Edit:

  • What date picker do you use?
    JQuery, but this is not a requirement, I can switch to any datepicker that can easily handle various yyyy-mm-dd save formats and display format.
  • What locale is your server in and what format do you prefer to work in your MVC code and save to the database?
    I do not know where my client will host the site, its AWS ... I prefer to work with en-us.
+7
c # datetime asp.net-mvc datepicker
source share
3 answers

Assuming your date picker can return the selected date to a Date () js object. I would suggest converting the date value to a standard format such as ISO8601. So you need to attach the onChange event to your date picker and convert the selected Date () value to ISO8601 format. You can assign this converted value to a hidden field (just date.toISOString ()).

The MVC.net analyzer also understands the ISO8601 format, so when your form data is published on the server, it is analyzed to correct the date value. See this question .

I created jsfiddle using jquery UI datepicker. It uses altFormat and altField, which can be used for the internal date format, regardless of which date format the user displays in the actual date input field.

Find jsfiddle here - https://jsfiddle.net/Prasoon1983/kyjvwkLd/2/

 $("#dob").datepicker({ altFormat: "yy-mm-dd", altfield:"#dobInternal" }); $( "#dob" ).datepicker( "option", "altField", "#dobInternal" ); 
 <script src="https://ajax.googleapis.com/ajax/libs/jqueryui/1.11.4/jquery-ui.min.js"></script> <input type="text" id="dob" /> <input type="text" id="dobInternal" /> 

I used an input control to display, but you can use hidden input.

Hope this helps.

+9
source share

Make your property below

  [RegularExpression(@"(\d{2}|\d{1})[/](\d{2}|\d{1})[/]\d{4}",ErrorMessage ="Birthday field must be ad dd/mm/yyyy format.")] public DateTime? BirthDate{ get; set; } 

Above regex only for dd / mm / yyyy format. If you need alternatives, you must add this sign to [/].

The birthdate property type for the ViewModel can be the string now.You can convert it to DateTime when you need to

+1
source share

Globalization is really complicated. But if you pass Javascrip, you can display the selected date in one format and send it to the server using another, for example. ISO standard.

On the server side, I would prepare a strict validator - for example. accept only date in ISO format. When you receive a request with some kind of strange input, respond with an error (with the corresponding message).

+1
source share

All Articles