1

I am trying to format date time attribute in MVC 4 class like this :

[Required]
[DataType(DataType.Date)]
[DisplayFormat(ApplyFormatInEditMode = true,DataFormatString = "{0:dd/MM/yyyy}")]
public DateTime DateOfBirth { get; set; }

but not work DateOfBirth attribute accept date format month before day (EX:accept 1/27/1990 and not accept 27/1/1990)

I am searching for answers and trying many solution but not work like

enter link description here

using html helper:

@Html.TextBoxFor(Model => Model.DateOfBirth) I appreciate any help thanks

Community
  • 1
  • 1
Amir
  • 101
  • 3
  • 12

2 Answers2

3

The [[DisplayFormat] attribute is only for how to display the value in the view. It has nothing to do with the format that will be accepted. You can write some code to accept a string and in the server parse that back into a proper DateTime property. Store your DateTime, then when you go to display it back to the user you use your [[DisplayFormat] attribute

what you would do is use Regular Expressions to verify the user inputed a number then "/" then number then "/" then number.

Obivously you are not the first one to run into something like this , this is why datepickers are so commnly used , they force the user to pick a date and format it appropiatly

Scott Selby
  • 9,420
  • 12
  • 57
  • 96
  • thanks this is a nice trick i will use it if there is no chance to change data format when insert so grateful @Scott – Amir Nov 12 '14 at 23:38
  • 1
    This is not entirely correct. Used in conjunction with `ApplyFormatInEditMode = true` and rendered with `@Html.EditorFor() the use of `[DisplayFormat]` is fine except that the format needs to be `"yyyy-MM-dd"` to correctly display in the browsers DatePicker control –  Nov 13 '14 at 00:02
2

If your using @Html.TextBoxFor() to render the control, then [DataType(DataType.Date)] and ApplyFormatInEditMode = true in the [DisplayFormat] attribute is not really necessary. These are only applicable when you use @Html.EditorFor() to render the browsers date picker, in which case the format must be "yyyy-MM-dd" in order to display correctly in all browsers.

If you want to display the formatted date in a textbox, use

@Html.TextBoxFor(Model => Model.DateOfBirth, "{0:dd/MM/yyyy}")

Note if you post back "27/1/1990" (27th January 1990) and the current culture of the server has the date format "MM/dd/yyyy" then you will need a custom model binder to correctly bind the date. Refer example here

Community
  • 1
  • 1
  • I am trying to insert data i just passing data to httppost method to insert this is a business class to validate attribute DateOfBirth attribute accept date in format month/day/year only – Amir Nov 13 '14 at 00:25
  • Not sure I understand. If you want the format to be `"MM/dd/YYYY"` why do you want to render it in the browser as `"dd/MM/yyyy"`? –  Nov 13 '14 at 00:29
  • No I want to format data in form of "dd/MM/yyyy" but when I am trying to insert data (error '27/01/1991' is not valid for DateOfBirth) – Amir Nov 13 '14 at 00:33
  • 1
    OK, that's because the servers date format is "MM/dd/yyyy" (probably just using the default InvariantCulture). You need to create a custom model binder. Refer to the link in my answer above. –  Nov 13 '14 at 00:39