ASP.NET MVC - DisplayFormat attribute
Asked Answered
P

2

0

In my model i've the MyDate property that has datetime type. I sign the property with the DisplayFormat attribute in this mode:

[DisplayFormat(ApplyFormatInEditMode = true, DataFormatString = "{0:dd/MM/yyyy hh:mm}")]
public DateTime MyDate { get; set; }

in my view:

...
<%= Html.EditorFor(model => model.Evento.MyDate)%>
...

why if the value of property is '2011-05-03 14:47', in my view (into EditorFor) i see '03/05/2011 02.47' ?

The DataFormatString is correct!

thanks so much for reply

Alberto

Phares answered 5/5, 2011 at 20:53 Comment(0)
B
6

Unless I'm mistaken, the format string of {0:dd/MM/yyyy hh:mm} will output as 03/05/2011 02.47. You're seeing what I would expect to see.

UPDATE: To get the 24 hour notation you can use {0:dd/MM/yyyy HH:mm} with the uppercase HH to designate the hour.

Belinda answered 5/5, 2011 at 21:1 Comment(2)
my problem is that the time is not formatted correctly. I expected 14:47 no 02.47...Phares
@Alberto Rubini: Ah, I see. I've updated my answer to reflect what you're trying to accomplish. Hope it works for you.Belinda
A
4

That's because : is a reserved character indicating the time separator for the given culture which in your case might happen to be the . character. You want:

DataFormatString = @"{0:dd/MM/yyyy HH\:mm}"

You probably might also want to use HH which is the 24 hour format instead of hh.

Asher answered 5/5, 2011 at 21:30 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.