Winforms .net Datepicker does not respect 24 hour format?

14,700

Solution 1

When you read the value, you'll need to use:

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

If you just use Value.ToString(), you'll get the default (12 hour) string formatting.

Solution 2

How are you getting hold of the entered time value from the control?

If you have a DateTimePicker control and set its Format property to Custom, and its CustomFormat property to HH:mm, you get a 24 hour format time (as a DateTime type) when you read the Value property.

EDIT: If you are using the ToString() value (as you say in the above comment), this will be using your current localisation settings. You would be much better of reading the values from the DateTime (ie. DateTime.Hour DateTime.Minute).

Share:
14,700
MickeyfAgain_BeforeExitOfSO
Author by

MickeyfAgain_BeforeExitOfSO

Updated on June 13, 2022

Comments

  • MickeyfAgain_BeforeExitOfSO
    MickeyfAgain_BeforeExitOfSO almost 2 years

    I have a timepicker (datetimepicker) formatted as HH:mm - 24 hour clock. It displays as 00 to 24 and 00 to 59 and will not allow invalid values to be entered. This is exactly what I want.

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

    I can test for and convert these unwanted return values, but surely(?) there is a more elegant way of persuading this thing to give me the values I want rather than checking for these special conditions? Some simple property that I've overlooked perhaps?