6

Following viewmodel used in a view is supposed to display a StartDate as, say 9/30/2015. But it is displaying as 9/30/2015 12:00:00 AM. How can I make it display without time while using DataAnnotaion? I know I can use @Model.StartDate.ToString("MM/dd/yyy") inside view to display date only. But that would mean you have to do it in every view that is using the following ViewModel:

ViewModel:

...
[DisplayFormat(DataFormatString = "{0:MM/dd/yyyy}")]
public DateTime StartDate { get; set; }
...

UPDATE

The corresponding model class of the above ViewModel already has the following DataAnnotation that correctly creates the data type in SQL Server table as Date; and when you run a query on the corresponding table in SSMS it correctly displays the StartDate column's data with dates only, say, 9/30/2015 etc.)

Model

...
[DataType(DataType.Date)]
public DateTime StartDate { get; set; }
...

StartDate in the sql db is in fact Date only. Moreover, if I run a query on SSMS it correctly returns date only.

nam
  • 21,967
  • 37
  • 158
  • 332
  • Have you tried just using the `[DataType(DataType.Date)]` attribute? – Erik Funkenbusch Jul 28 '17 at 17:05
  • @ErikFunkenbusch To answer your question, I've added an **UPDATE** section to my post. Moreover, I did try your suggestion on the ViewModel as well but to no avail. – nam Jul 28 '17 at 18:09
  • 1
    Using the DataType attribute should work if your rendering with EditorFor or DisplayFor. – Erik Funkenbusch Jul 28 '17 at 18:34
  • FYI, here's a fiddle that demonstrates https://dotnetfiddle.net/urdrHM – Erik Funkenbusch Jul 28 '17 at 19:02
  • @ErikFunkenbusch In my `view` I'm using `@Model.StartDate`. Per your suggestion I should be using `DisplayFor` instead - correct? – nam Jul 28 '17 at 19:26
  • Probably. If you just reference the variable, it's not going to use your data attributes. You need to use DisplayFor or EditorFor to recognize them. Otherwise you would have to do @Model.StartDate.Format("d") – Erik Funkenbusch Jul 28 '17 at 19:39
  • @ErikFunkenbusch But `@Model.StartDate` is still displaying the date except that it's including default time as well such `9/30/2015 12:00AM`. And `VS2017` does not recognize `@Model.StartDate.Format("d")` – nam Jul 28 '17 at 20:04
  • Let us [continue this discussion in chat](http://chat.stackoverflow.com/rooms/150435/discussion-between-nam-and-erik-funkenbusch). – nam Jul 28 '17 at 20:09

1 Answers1

11

There are two solutions to your problem. As per your comments, you are currently using @Model.StartDate to display a date.

You can either do this:

@Model.StartDate.ToString("d")

Or, you can use a model based approach in your ViewModel and do this:

[DataType(DataType.Date)]
public DateTime StartDate {get;set;}

Then, in your view use:

@Html.DisplayFor(m => m.StartDate)
Erik Funkenbusch
  • 92,674
  • 28
  • 195
  • 291
  • Last comment that you added in our [chat room](https://chat.stackoverflow.com/rooms/150435/discussion-between-nam-and-erik-funkenbusch) may answer my [this](https://stackoverflow.com/q/45382084/1232087). If you like you can write a response there and I'll mark that as an answer. – nam Aug 01 '17 at 01:23