Search code examples
c#parsingdatetimetimepicker

TimePicker Plugin Making It Difficult To Parse String


I am trying to parse a time in string format to a DateTime using

var from = DateTime.Parse(TimeFrom);

When the string was like this 7:25 PM it worked

I then switched to a Timepicker plugin which displays the time like this 7 : 25 : AM

and it no longer works?

How do I change 7 : 25 : AM to 7:25 AM


Solution

  • TimeFrom = TimeFrom.Replace(" : ", ":").Replace(":A", " A").Replace(":P", " P");
    

    ^^ Quick and dirty way to do it.

    But I can't understand why the timepicker control would return such an unusually formatted time. Perhaps there is a way to configure it to return the time in a more normal format?

    Another approach is to use DateTime.ParseExact(... ) and specify a format string that describes the format you are seeing.