0

When I edit my one of the form sin ASP.NET MVC 3 in this when I edit a user registration form then I got date in startdate is 21-Mar-12 12:00:00 AM in text box but I need 21-Mar-12.

So how can I format textbox date like that?

Peter O.
  • 32,158
  • 14
  • 82
  • 96
andy
  • 595
  • 3
  • 8
  • 23

1 Answers1

1

You could decorate the DateTime property on your view model with the [DisplayFormat] attribute which allows you to specify a given format:

[DisplayFormat(ApplyFormatInEditMode = true, DataFormatString = "{0:dd-MMM-yy}")]
public DateTime SomeDate { get; set; }

and in your strongly typed view use the EditorFor helper to render the corresponding input field:

@Html.EditorFor(x => x.SomeDate)

As far as binding the value back to a DateTime field when posting back is concerned you could write a custom model binder that will use this format. I have shown an example here.

Community
  • 1
  • 1
Darin Dimitrov
  • 1,023,142
  • 271
  • 3,287
  • 2,928
  • Hello Darin thanx for quick reply but when i'm using this synyax then it shows date: "00-21-12" this.. – andy Mar 27 '12 at 07:22
  • it works with @Html.TextBox("EndDate", string.Format("{0:dd-MMM-yyyy}", Model.EndDate), new { @class = "EndDate" }) – andy Mar 27 '12 at 08:42