6

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

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

and in my view (is using the 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 one setting on my javascript datepicker is:

format: 'L'

because I want show dates without times.

Now, I can't save, because of the validation error:

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

Now, I KNOW, its a globalization error... I need set culture in <globalization> element in web.config, etc... But, have you noticed I set no formats (except the datepicker format "L")? I want that form get birthdates from people in any country, no a specific culture/format etc.

Is this possible? How can I do it?

Edit:

  • which date picker are u using?
    The JQuery one, but this is not a requeriment, I can change to any datepicker that can easily handle different save formats "yyyy-mm-dd" and display format.
  • Which locale your server is located and which format do you prefer to work within your MVC code and to persist in database?
    I dont know where my customer will host the site, its AWS... I prefer work with en-us.
Click Ok
  • 8,700
  • 18
  • 70
  • 106
  • 2
    Then use `yyyy-MM-dd` format. this applies globally and cant have any ambiguity – Nkosi Dec 27 '16 at 00:53
  • 1
    which date picker are u using? it doesn't seems to be JQueryUi one? – pgcan Dec 27 '16 at 06:20
  • 1
    Which locale your server is located and which format do you prefer to work within your MVC code and to persist in database? May be this [Globalization jQuery Plugin from ScottGu's blog](https://weblogs.asp.net/scottgu/jquery-globalization-plugin-from-microsoft) could help you. – Siva Gopal Dec 27 '16 at 07:14
  • @Prasoon and @"Siva Gopal" -> answers on edits – Click Ok Dec 27 '16 at 15:28
  • 1
    You absolutely either need to *know* the format you're getting the value in (ie. the format has to be supplied alongside the value) or you need to *require* a specific format. Otherwise, what date is `10/11/12` going to be? – Lasse V. Karlsen Dec 28 '16 at 07:14

3 Answers3

9

Assuming that your datepicker can return the selected date in Date() js object. I would suggest to convert the date value in to a standard format like ISO8601. So what you have to do is to attach a onChange event on your date picker and convert the selected Date() value into ISO8601 format. You may assign this converted value to a hidden field (simply date.toISOString()).

MVC .net parser also understand ISO8601 format so when your form data is posted on server it is parsed to correct date value. See this question also.

I have create a jsfiddle using jquery UI datepicker. It uses a altFormat and altField which can be used for internal date format irrespective of whatever date format is shown to user in actual date input field.

Find the 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 have used an input control for display purpose but you can use a hidden input.

Hope it helps.

Community
  • 1
  • 1
pgcan
  • 1,199
  • 14
  • 24
1

Make your property as 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 that sign in [/].

Type of birthdate property of ViewModel can be string now.You can convert it to DateTime when you need

Mehmet
  • 739
  • 1
  • 6
  • 17
1

Globalization is really hard. But if you relaying on Javascrip then you can display selected date in one format and send it to server using different one e.g. ISO standard.

On server side I would prepare strict validator - e.g. accept only date in ISO format. When you receive request with some strange input then respond with error (with proper message).

Piotr Pasieka
  • 2,063
  • 1
  • 12
  • 14