Winforms.net Datepicker does not support 24-hour format?

I have a timepicker (datetimepicker) formatted as HH: mm - 24 hour. It displays from 00 to 24 and from 00 to 59 and does not allow input of invalid values. This is exactly what I want.

However, it returns values ​​like 12 hours with the AM and PM indicators. This means that when the user enters “00”, he returns as “12” and “14” returns as “2:00”. This is NOT what I want.

I can check and convert these unwanted return values, but surely (?) Is there a more elegant way to convince this thing to give me the values ​​I want, and not check these special conditions? Some simple property that I may have forgotten?

+3
source share
2 answers

When you read the value, you will need to use:

string value = dateTimePicker.Value.ToString("HH:mm");

If you just use Value.ToString(), you will get the default string formatting (12 hours).

+4
source

How do you get an available time value from a control?

If you have a control DateTimePickerand set its Formatproperty Custom, and its CustomFormatbefore HH:mm, you get a 24-hour time format (as a type DateTime) when you read Value.

EDIT: ToString() ( ), . DateTime (.. DateTime.Hour DateTime.Minute).

+3

Source: https://habr.com/ru/post/1751673/


All Articles