Forum Xamarin.Forms
We are excited to announce that the Xamarin Forums are moving to the new Microsoft Q&A experience. Q&A is the home for technical questions and answers at across all products at Microsoft now including Xamarin!

We encourage you to head over to Microsoft Q&A for .NET for posting new questions and get involved today.

TimePicker doesn't respect local setting (24hr formatting)

PhilippSumiPhilippSumi USMember ✭✭✭

Hi there

My phone (Android) has its locale set to en-us, but time on my phone is displayed in 24 hour format (as configured on the phone). This is respected by every single app on my phone.

However, I noticed today that when displaying a TimePicker on my Xamarin Forms app, Xamarin displays a 12 hr time picker (probably according to the system locale).

That's the first problem. Next is to actually render the time on the UI. Here again, Xamarin fails with a ToString("t") - not surprisingly since this is just based on the UI CultureInfo. So I would need a device-specific time formatting method or the device configuration.

Showing a picker and displaying something as typical as time on a UI is a feature I would expect to just work out of the box. Is there anything in Xamarin Forms I overlooked? And if not, is this a known issue (that hopefully will be fixed soon)?

Thanks,
Philipp

Posts

  • JohannesHerterJohannesHerter USMember ✭✭
    edited October 2016

    @PhilippSumi
    Xamarins TimePickerRenderer does always show a TimePicker with 12h format. In the sources you can see the line
    _dialog = new TimePickerDialog(Context, this, view.Time.Hours, view.Time.Minutes, false);, the name of the boolean parameter at the end is is24HourView. As the name suggests, this parameter changes the time format. The only workaround I know to fix this is to write your own TimePickerRenderer (you can basically copy the original Xamarin renderer and adjust the OnClick method).

    What exactly do you mean with "Xamarin fails with a ToString("t")"?

    I'm not sure what time the default TimePicker shows in the UI, but you can add a Format to the TimePicker (e.g. in xaml Format="hh:mm tt").

  • PhilippSumiPhilippSumi USMember ✭✭✭
    edited October 2016

    Hi Johannes

    Thanks for the feedback! That hardcoded value is horrible - but at least I see a workaround there. Now I just need to find out how to determine the time format. I saw that I can access the system setting in Android, should be possible on iOS, too).

    The ToString("t") remarks refers to also rendering the time in the "wrong" format. This doesn't refer to the TimePicker, but actually just displaying time on the UI. Given that pretty much any app I use displays times at some point, I'm very surprised that Xamarin Forms doesn't seem to give me the tools to easily do that correctly. I'll post back once/if I have a working solution for all platforms.

    (small note: hardcoding that boolean in the renderer is just very, very sloppy. Shame on that programmer ;)

  • PhilippSumiPhilippSumi USMember ✭✭✭

    Just stumbled over that old thread of mine. I made a PR on this and the fix is in the current 2.3.4.184-pre1 preview, so it should be fixed in stable soon.

Sign In or Register to comment.