question

IrinaKushnirik-7591 avatar image
0 Votes"
IrinaKushnirik-7591 asked LeonLu-MSFT edited

TimePicker posts 12-hour formatted value in the control when device setting is 24-hour format

On both Android and iOS TimePicker picker seems to respect device settings (24/12-hour format). However, when TimePicker posts value from the picker into the control, value appears in the 12-hour format regardless of the device setting. Is there a TimePicker.Format value, which will respect device settings (24/12-hour format)?

dotnet-xamarin
5 |1600 characters needed characters left characters exceeded

Up to 10 attachments (including images) can be used with a maximum of 3.0 MiB each and 30.0 MiB total.

1 Answer

LeonLu-MSFT avatar image
0 Votes"
LeonLu-MSFT answered LeonLu-MSFT edited

Hello,​

Welcome to our Microsoft Q&A platform!

Please use Format to control it. If you set it like following code format, the time is 24 hour format.

<TimePicker Format="HH:mm"></TimePicker>



=========================Update===============================

You can use dependService to get the current system time settings if 12/24 hours format.

Create an interface called ITimer.

public interface ITimer
    {
        bool IsTime12();

    }


Then achieve it in android and iOS.

For Android.

using Android.App;
using Android.Content;
using Android.OS;
using Android.Runtime;
using Android.Views;
using Android.Widget;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using TimePicker2412.Droid;
using Xamarin.Forms;

[assembly: Dependency(typeof(TimerService))]
namespace TimePicker2412.Droid
{
    class TimerService : ITimer
    {
        public bool IsTime12()
        {
            //throw new NotImplementedException();

            ContentResolver cv = Android.App.Application.Context.ContentResolver;
            String strTimeFormat = Android.Provider.Settings.System.GetString(cv,
                                               Android.Provider.Settings.System.Time1224);

            if (strTimeFormat.Equals("24"))

            {
                return false;
            }
            else
            {
                return true;
            }
        }
    }
}


For iOS.

using Foundation;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using TimePicker2412.iOS;
using UIKit;
using Xamarin.Forms;

[assembly: Dependency(typeof(TimerService))]
namespace TimePicker2412.iOS
{
    class TimerService : ITimer
    {
        public bool IsTime12()
        {
         

            var dateFormatter = new NSDateFormatter();
            dateFormatter.DateStyle = NSDateFormatterStyle.None;
            dateFormatter.TimeStyle = NSDateFormatterStyle.Short;

            var dateString = dateFormatter.ToString(NSDate.Now);
            var isTwelveHourFormat =
            dateString.Contains(dateFormatter.AMSymbol) ||
            dateString.Contains(dateFormatter.PMSymbol);
            return isTwelveHourFormat;
        }
    }
}


When we use TimePicker, we can judge the current system settings. If the system setting is 24 hours. We can set TimePicker.Format in the background code.

if (!DependencyService.Get<ITimer>().IsTime12()) {

                MyTimePicker.Format = "HH:mm";


            };


Best Regards,

Leon Lu



If the response is helpful, please click "Accept Answer" and upvote it.

Note: Please follow the steps in our documentation to enable e-mail notifications if you want to receive the related email notification for this thread.


· 11
5 |1600 characters needed characters left characters exceeded

Up to 10 attachments (including images) can be used with a maximum of 3.0 MiB each and 30.0 MiB total.

@IrinaKushnirik-7591
May I know if you have got any chance to check my answer?

0 Votes 0 ·

Hi, thank you for response.

I need the control value to be in 12-hour format, when device setting is set to 12-hour format and in 24-hour format, when device setting is set to 24-hour format.

0 Votes 0 ·

At this stage I do not see how I can do this without extra code on my part: I need to check the device setting based on the platform and assigning Format value according to it.

And I think there is a bit of room for improvement in that. For example, the default Format value could have been empty and in that case the control value can be formatted according to the device settings. Otherwise, use the Format value.

0 Votes 0 ·

I am pretty sure that this discussion is what I am talking about.

timepicker-doesnt-respect-local-setting-24hr-formatting

The discussion was about a picker/dialog to select value from (which was later fixed) and rendering the value (Control.Text). Control.Text is still using default “t”, which uses CultureInfo and does not consider device 12/24-hour setting.

So, as I mentioned, to make TimePicker reflect device 12-24-hour setting, I need to write code to get the setting and then set TimePicker.Format, based on the device setting. Would have been easier if TimePicker had that code in-built: e.g. TimePicker.Format default value is empty and the Control.Text is rendered based on the device 12/24-hour format.


0 Votes 0 ·

Please see my updated answer.

0 Votes 0 ·
Show more comments