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?

1 Answer 1

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.

Sign up to request clarification or add additional context in comments.

2 Comments

Hello Darin thanx for quick reply but when i'm using this synyax then it shows date: "00-21-12" this..
it works with @Html.TextBox("EndDate", string.Format("{0:dd-MMM-yyyy}", Model.EndDate), new { @class = "EndDate" })

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.