Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to use TimeSpan string format in code in Xamarin.Forms?

I am using a Label in Xamarin.Forms in code like so:

var label = new Label();
label.SetBinding(Label.TextProperty, new Binding("Time", stringFormat: "{}{0:hh\\:mm}", mode: BindingMode.TwoWay, source: this));

But this returns the error:

System.FormatException: Input string was not in a correct format.

But this works in Xaml:

 <Label Text="{Binding StartTime, StringFormat='{}{0:hh\\:mm}'}}"/>

How do I use string format on a binding for TimeSpan in Xamarin.Forms?

like image 303
User1 Avatar asked Sep 18 '25 01:09

User1


1 Answers

Xamarin Forms uses all the basic string.Format options you would normally use. So for a datetime the stringFormat variable would look like this:

"{0:MM/dd/yy H:mm:ss zzz}"

The additional pair of brackets in your format string seem out of place to me. You could try the following for what you're trying to achieve here:

@"{0:hh\:mm}"
like image 121
Steven Thewissen Avatar answered Sep 20 '25 16:09

Steven Thewissen