19

I have my model field decorated in the following way:

[DataType(DataType.Date)]
[Display(Name = "Date of birth")]
public string DateOfBirth { get; set; }

When I want to display the value in the view using the following code:

<%: Html.DisplayFor(m => m.DateOfBirth) %>

The problem is that the date is displayed together with its time value. I wonder why it does not take DateType attribute into consideration and displays only the date value without time. I know that I may create a display template for DateTime but in other cases than date of birth I want to show time together with date. How to solve the problem?

dove
  • 20,469
  • 14
  • 82
  • 108
GUZ
  • 627
  • 1
  • 9
  • 18

7 Answers7

17

The problem you have here is that you are using a string value rather than a DateTime.

change your model to be:

[DataType(DataType.Date)]
[Display(Name = "Date of birth")]
public DateTime? DateOfBirth { get; set; }

DataType will only work if it's a DateTime type, you also get the added advantage that it will automatically validate it as a valid date when using a DateTime. If you use string, you will have to use a regular expression validator to ensure a proper date has been entered.

Erik Funkenbusch
  • 92,674
  • 28
  • 195
  • 291
  • I had a sitation recently where chrome put funny stuff in when input type is date, part of html5 i think, I wanted to use jquery datepicker instead so I went with DataType.Text - that still allowed me to use formatting cool enough – John Sep 01 '13 at 12:12
  • @John - "funny stuff"? That's part of the html standard. If you're using html5, you should use the html5 input types. Chances are, you just didn't understand something, and rather than do the correct thing, you've now broken MVC's date handling. – Erik Funkenbusch Sep 01 '13 at 17:49
  • no I mean Chrome adds datepickers that doesn't look as nice as jquery's versions which I prefer – John Sep 03 '13 at 10:18
13

This should do it for edit mode and display

 [DisplayFormat(ApplyFormatInEditMode = true,DataFormatString = "{0:MM/dd/yyyy}")]

though if it is just display, this might work

[DisplayFormat(DataFormatString = "{0:d}")]
dove
  • 20,469
  • 14
  • 82
  • 108
  • @MystereMan i'm thinking you know what you're at. (this).it may not be necessary but surely you know you have to be more specific than your comments here. what do they add to the general knowledge? i will vote for you better answer. – dove Oct 20 '12 at 22:42
  • @MystereMan so the SO cache bursting is not perfect perhaps. see it now. – dove Oct 21 '12 at 07:05
  • Spectacular! So simple and solved my problem. Thanks! – Rui Craveiro Jul 31 '14 at 00:41
  • Brilliant, I was banging my head with this one. – Jon649 Mar 01 '23 at 15:31
6

You can use ToShortDateString() or ToLongDateString() to display date only,example:

@Model.EventDate.ToShortDateString()
@Model.EventDate.ToLongDateString()
zallexy
  • 131
  • 1
  • 2
  • 7
6

Use DisplayFormatAttribute to indicate format when value is displayed. Also, you could create two DisplayTemplates, Date and DateTime, and use UIHintAttribute to specify template

archil
  • 39,013
  • 7
  • 65
  • 82
2

If your date is of type DateTime, and ToShortDateString() or ToLongDateString() still doesn't work, check if your DateTime is nullable (DateTime?). Making it not nullable can do the trick.

Robert Columbia
  • 6,313
  • 15
  • 32
  • 40
1

If you are using MVC, perhaps you can try this on your client side script:

@{
      string birthDateOnly = Model.DateOfBirth.Date.ToShortDateString();
 }
@Html.DisplayTextFor(m => birthDateOnly)
Toshihiko
  • 325
  • 1
  • 8
  • 20
0

using @Value and ToShortDateString() you can display only date.

@Html.TextBoxFor(model => model.StartDate, "", new { id = "date", @class = "datepicker", @style = "width:70%; height:30px;", @placeholder = "Enter Date", @Value = @Model.StartDate.ToShortDateString()})