0

In my mvc application, i assign the Date to my view using DisplayFor Htmlhelper, like below. I tried to format like this. But, i am not successful.

@Html.DisplayFor(d => d.TravelAllowances.DepatureDate, "{0:dd-MM-yy}")

Please suggest me some way to accomplish this.

Note: DepartureDate is in String format.

Thanks Manikandan

Martin Maillard
  • 2,751
  • 19
  • 24
Manikandan
  • 589
  • 1
  • 9
  • 18

2 Answers2

0

Do you have to use Html.Display? Why not render it directly? @Model.TravelAllowances.DepatureDate.ToString("dd-MM-yy");

ojlovecd
  • 4,812
  • 1
  • 20
  • 22
0

I know this question is three years old and the OP probably found a way, but in case others stumble upon this Q/A...

The problem is that you have the date as just a string.

If you are storing it as a date and converting it to a string when populating the model being passed to the view from the controller, then format it as you want in the controller.

If you are storing date as a string (bad idea IMHO), and just populating the model property in the controller, then I would parse the string into a DateTime object and then format that DateTime into a string for the Model's property.

Just my two cents, three years late :)

MikeScott8
  • 720
  • 2
  • 10
  • 17