1

I have the following code in my model:

[DisplayFormat(DataFormatString = "{0:HH:mm}", ApplyFormatInEditMode = true)]
[DataType(DataType.Time)]
public DateTime Start { get; set; }

And in my cshtml form:

<input asp-for="Start" class="form-control" asp-format="{0:HH:mm}" />

In Chrome and Edge the input displays 11:00 PM instead of 23:00. I expect that HH means 00-23, at least as far as I understood.

This is what Chrome tells me if I put a wrong format, e.g. {0:something}.

The specified value "12:00 AM" does not conform to the required format. The format is "HH:mm", "HH:mm:ss" or "HH:mm:ss.SSS" where HH is 00-23, mm is 00-59, ss is 00-59, and SSS is 000-999.

I'm not sure where I'm going wrong. How can I get 24h notation?

maccettura
  • 10,514
  • 3
  • 28
  • 35
fonZ
  • 2,428
  • 4
  • 21
  • 40
  • What is the zero for in the format? The zero is for the index number of the parameter. Where is the parameter? – jdweng Oct 02 '19 at 17:00
  • @silentw How is this a duplicate? The question you refer to as giving the answer is only talking about a HTML tag. In my case it's a asp.net core 2.2 form in a cshtml file. The answers given mostly involve javascript while this is not needed here. I already get a time picker, without javascript. The problem is that it displays AM/PM time, not 24h time while the format given clearly says otherwise. This answer https://stackoverflow.com/questions/47700287/how-to-format-date-time-number-in-asp-net-core-2-0-razor-tag-helpers?rq=1 suggests that this should work, but it doesn't. – fonZ Oct 02 '19 at 17:57
  • @jdweng I'm not following ... – fonZ Oct 02 '19 at 18:00
  • 1
    This is probably due to your culture settings/datetime preferences in Windows. It worked for me, displaying time in `HH:mm` format. Then I changed my system preference to `hh:mm tt` ie `11:00 PM` and it displayed the way you describe. EDIT: I'm using chrome – haldo Oct 02 '19 at 18:10
  • @haldo wow great man, thanks! I would not have thought about that. Changed the regional settings and it magically worked. – fonZ Oct 02 '19 at 18:25

0 Answers0