Search code examples
c#datetimedatetimepicker

Assign time stored as a string to the Value property of a DateTimePicker


I have a time value that I am reading from a db that is in military format with "." as the separating character (Format: "HH.mm.ss" Example: "15.32.18").

What is the best way for me to display this string using a DateTimePicker. The date time picker is formated as follows:

timePicker.Format = DateTimePickerFormat.Time;
timePicker.CustomFormat = "HH.mm.ss";

The only way I can think of is to create a DateTime object with the time value that is stored in the string and assign that to my timePicker. This seems to be a backwards way of doing it.


Solution

  • The method you're looking for is DateTime.ParseExact:

    DateTime.ParseExact("15.32.18", "HH.mm.ss", null)
    

    gives a date with the time portion 3:32:18 PM.