Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Winforms .net Datepicker does not respect 24 hour format?

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?

like image 503
MickeyfAgain_BeforeExitOfSO Avatar asked Jun 24 '10 22:06

MickeyfAgain_BeforeExitOfSO


2 Answers

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).

like image 92
adrianbanks Avatar answered Sep 29 '22 05:09

adrianbanks


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.

like image 34
Reed Copsey Avatar answered Sep 29 '22 04:09

Reed Copsey