1

I am developing the MVC 3 application. In the view I am displaying the name along with created date time. It displayed as a

Nick Moris 27-Mar-13 11:49 AM <--

but I want to display in

Nick Moris 27-Mar-13 11:49 am <--

I have write the below line of code in the partial class for the date time property.

   [DisplayFormat(DataFormatString = "{0:dd MMM yy hh:mm tt} ")]
   public System.DateTime CreateDateTime { get; set; }

and in the View I am using below code...

@Html.DisplayFor(ModelItem => item.CommentDateTime.ToString("dddd, MMMM d, yyyy a\\t h:mmtt"))

What changes I have to made in the date format ? It showing error.

tereško
  • 58,060
  • 25
  • 98
  • 150
Nil
  • 133
  • 2
  • 7
  • 23
  • 1
    possible duplicate of [Get AM/PM for a date time in lowercase using only a datetime format](http://stackoverflow.com/questions/499393/get-am-pm-for-a-date-time-in-lowercase-using-only-a-datetime-format) – Daniel Imms Mar 27 '13 at 06:53
  • 2
    Above solution is not working at all...giving error... – Nil Mar 27 '13 at 07:04
  • It seems you have two problemas here. Why do you have two different formats, one in the view model and one in the view? Which one do you want? – Ivo Feb 11 '14 at 20:22

1 Answers1

6

You could format it in the view without using the DisplayFor helper:

@item.CommentDateTime.ToString("dd-MMM-yy hh:mm")  @item.CommentDateTime.ToString("tt").ToLower()

In this case you no longer need the DisplayFormat attribute on your view model.

Darin Dimitrov
  • 1,023,142
  • 271
  • 3,287
  • 2,928
  • This is actually a good solution for the issue. Just replace the `@Html.DisplayFor` part with this – Ivo Feb 11 '14 at 20:23