Search code examples
xamarinxamarin.formsformattimepicker

Xamarin Forms Timepicker Format Input String not in the correct format


I have a hopefully simple question for you. I created a Timepicker for my Xamarinapp. This Timepicker should get the 24hours format. So in den .xaml File I did that.

<TimePicker Format="HH:mm" x:Name="Time"/>

In the view its no problem, but whenever I try to save it in that format in my code I get the error "Input string was not in a correct format".

My code looks like this

time = Time.Time.ToString(Time.Format)

Maybe someone of you sees the Problem, because I don't get it.

Thanks for the help and Cheers!


Solution

  • The correct format should be:

       string time = Time.Time.ToString(@"hh\:mm");
    

    You can check the valid format in this section:

      string[] fmts = { "c", "g", "G", @"hh\:mm\:ss", "%m' min.'" };
    

    And you can check document about the standard-timespan-format-strings and custom-timespan-format-strings

    Here are also some links you can refer:

    time-picker-as-24-hours-format-in-xamarin-forms

    24-hours-time-picker-selected-time