20

I would like to do something like

model.PickupDate.ToString("d")

But MVC4 is not liking that very much. PickupDate is a DateTime field and I would like to strip off the time portion when displayed in the view, while keeping the new { id = "date1" } code which binds the TextBoxFor to the javasript datepicker. How can I display the Date portion only in this instance?

@Html.TextBoxFor(model => model.PickupDate, new { id = "date1" })

7 Answers 7

30

this is how I would do this. Hope it helps

 @Html.TextBoxFor(m => m.MyDateTime, new { Value = Model.MyDateTime.ToString("MM-dd-yyyy"), id = "MySuperCoolId"});

or in your case

@Html.TextBoxFor(m => m.PickupDated, new { Value = Model.PickupDate.ToString("d"), id = "date1"});
Sign up to request clarification or add additional context in comments.

Comments

28
+50

This should work:

@Html.TextBoxFor(model => model.SomeDateTime, "{0:MM/dd/yyyy}", new { id = "yourID" })

1 Comment

This is the method (with minor edits due to vb) that finally worked for me using razor & vb.net thanks!
4

Where you define your model you can supply the date formatting, in this example I have DD/MM/YYYY as my format, but you can do whatever format you need.

Model Code

public class MyModel
{
   [DisplayFormat(DataFormatString = "{0:dd/MM/yyyy}", ApplyFormatInEditMode = true)]
   public DateTime SomeDateTime {get;set;}
}

Then your razor

@Html.EditorFor(model => model.SomeDateTime, new { id = "date1" })

6 Comments

Sorry, that doesn't solve the issue. I added the DisplayFormat attribute you mentioned above but the Edit view still shows the complete DateTime format with the 12:00 AM appended to the date. Since it's a DateTime format in the sql express database its getting saved as 7/18/2013 12:00:00 AM format, and then the Edit view shows it just like that.
@AustinHarris made a quick change to the razor and attribute. What I had was for display rather than edit
If I change to the EditorFor then the date does get displayed in the correct format defined by the Model attribute you listed above. However, the JavaScript datepicker code won't hook into the EditorFor like it does with the TextBoxFor. :(
<script type="text/javascript"> $(document).ready(function () { $("#date1").datepicker({ dateFormat: 'mm/dd/yy' }); $("#date2").datepicker({ dateFormat: 'mm/dd/yy' }); }); </script>
Are you able to create a jsfiddle.net showing the javascript problem? (If you use Firefox, rightclick the editor for the date and choose inspect element to get the generated HTML node)
|
4

Based on https://stackoverflow.com/a/13702321/787511, tested with JQueryUI datepicker.

@Html.TextBoxFor(model => model.PickupDate, "{0:d}", new { id = "date1" })

Comments

0

If you make conversion only on the View, you can write this as follow

@Html.TextBoxFor(model => model.SomeDateTime, String.Format("{0:MM/dd/yyyy}"))

1 Comment

Hi, but I still need the new { id = "date1" } code in the view to use the javascript datepicker. This code does not work: @Html.TextBoxFor(model => model.PickupDate, String.Format("{0:MM/dd/yyyy}"), new { id = "date1" })
0

Display will depend on culture. And while in most cases all other answers are correct, it did not work for me. Culture issue will also cause different problems with jQuery datepicker, if attached.

If you wish to force the format escape / in the following manner:

@Html.TextBoxFor(model => model.PickupDate, "{0:MM\\/dd\\/yyyy}", new { id = "date1" })

If not escaped for me it show 08-01-2010 vs. expected 08/01/2010.

Also if not escaped jQuery datepicker will select different defaultDate, in my instance it was May 10, 2012.

Comments

0
   @Html.TextBoxFor(model => model.Date, String.Format("{0:dddd, MMMM d, yyyy}",Model.Date),  new { @class = "form-control"})

2 Comments

Please add some explanations as well.
just add some explanation to this answer as well .. Thank You :)

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.