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" })
Lotok
  • 4,517
  • 1
  • 34
  • 44
Austin Harris
  • 5,150
  • 6
  • 26
  • 39

7 Answers7

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"});
Kenneth Garza
  • 1,886
  • 14
  • 12
28

This should work:

@Html.TextBoxFor(model => model.SomeDateTime, "{0:MM/dd/yyyy}", new { id = "yourID" })
Rikin Patel
  • 8,848
  • 7
  • 70
  • 78
Yiding
  • 2,914
  • 3
  • 21
  • 16
  • This is the method (with minor edits due to vb) that finally worked for me using razor & vb.net thanks! – daveomcd Sep 15 '14 at 15:34
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" })
Lotok
  • 4,517
  • 1
  • 34
  • 44
  • 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. – Austin Harris Jul 25 '13 at 08:44
  • @AustinHarris made a quick change to the razor and attribute. What I had was for display rather than edit – Lotok Jul 25 '13 at 08:53
  • 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. :( – Austin Harris Jul 26 '13 at 08:13
  • – Austin Harris Jul 26 '13 at 08:13
  • Are you able to create a http://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) – Lotok Jul 26 '13 at 08:18
  • I assume the datepicker issue was from EditorFor creating a different `id` (the property name). This is resolved by either setting the id, as edited here, or adjusting your script to use the new id (my preference). – brichins Nov 06 '15 at 00:07
4

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

@Html.TextBoxFor(model => model.PickupDate, "{0:d}", new { id = "date1" })
Community
  • 1
  • 1
D.Rosado
  • 5,634
  • 3
  • 36
  • 56
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}"))
Elvin Mammadov
  • 25,329
  • 11
  • 40
  • 82
  • 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" }) – Austin Harris Jul 25 '13 at 08:29
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.

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