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

Irina Kushnirik 11 Reputation points
2021-05-13T10:01:15.193+00:00

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)?

Xamarin
Xamarin
A Microsoft open-source app platform for building Android and iOS apps with .NET and C#.
5,261 questions
0 comments No comments
{count} votes

1 answer

Sort by: Most helpful
  1. Leon Lu (Shanghai Wicresoft Co,.Ltd.) 64,571 Reputation points Microsoft Vendor
    2021-05-13T11:55:23.747+00:00

    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.