Show Time in 12 and 24 hour format in UIDatepicker on the basis of app settings not on device Settings
Asked Answered
I

12

33

My problem is there is a switch in my app which toggles for 12 hour and 24 hour time format.I am displaying my labels time according to that, but the UIDatePicker time is not getting changed.It's time is depending on device time settings format.Is it possible to show time in UIDatePicker according to my app settings which might be different from device time settings. Somewhere I read that UIDatePicker respects only device settings but still I want to ask if it is possible to show time in 12 and 24 hour format in UIDatePicker based on my app settings.

Please help me. Any suggestions would be highly appreciated!

Inkster answered 22/6, 2012 at 6:32 Comment(1)
I have post an answer for workaround. instead of changing it through locale. convert the value after the user has select.Bing
S
23

For UIDatePicker you cannot change the time format to 24 or 12, it depends on the user local settings in setting.app

Sandisandidge answered 22/6, 2012 at 6:37 Comment(4)
@AlmasAdilbek could you mention how we can?! that's already mentioned in the UIDatePicker - Internationalization that changing the local would constraint the format.Orleans
@AhmadF Commin back from '16. Actually, look at the answer below ↓, initializing the NSLocale with the identifier, let's say kk_Cyrl_KZ will return 24-hour format.Revolute
@AlmasAdilbek which means that the format would be based on the assigned local. Check this answer.Orleans
@AlmasAdilbek Actually theres a work around for this. but, its only converting the value after it has been selected. not while selected. since the user device are in 12h when selecting time it preferable to used 12h as the user already familiar with the format. but can convert the value afterward by using dateformatter.Bing
B
38

Try this code to show 24 Hrs Format in DatePicker:

NSLocale *locale = [[NSLocale alloc] initWithLocaleIdentifier:@"NL"];
[self.datePicker setLocale:locale];
Bernoulli answered 27/2, 2013 at 6:52 Comment(8)
Locale NL is the Netherlands, and in the Netherlands the time is always expressed in 24-hour format. The OS recognises this and provides a suitable picker. Personally I do not recommend changing the format of the date picker since it should function as the user requests.Bleak
Wrong solution. It could result in time errors on DateTime picker since the picker is configured on the Netherlands locale!Un
Maybe it works in English, but setting fixed locale is bad ideaMalloy
@Un how so? Can you provide an example? Could the errors be prevented somehow?Expertism
@RaduSimionescu wrong solution because the only acceptable solution is the accepted one: the hour format only depends on the locale, which can be configured in user settings. Thus, writing @"NL" to force 24Hrs format is nothing other than a code smell :)Un
ahah @RaduSimionescu, I just don't like using magic numbers (or magic strings) in my code :)Un
Unfortunately in iOS 14 it's not working. Changing the locale doesn't change anything in simulator.Masaryk
I filed a bug at Apple about this. When setting a 24-hour locale, and you're past 13:00 it hides the AM/PM selection correctly, and shows the current hour. But when I rotate it, it only shows 1-12 + the current selected hour like 16.Masaryk
E
32

In Swift

    // myDatePicker is yourUIDatePicker Outlet
    @IBOutlet weak var myDatePicker: UIDatePicker!

    // For 24 Hrs 
    myDatePicker.locale = NSLocale(localeIdentifier: "en_GB") as Locale

    //For 12 Hrs
    timePickerView.locale = NSLocale(localeIdentifier: "en_US") as Locale
Eft answered 25/11, 2015 at 9:30 Comment(1)
this no longer works in iOS16.5Felonry
S
23

For UIDatePicker you cannot change the time format to 24 or 12, it depends on the user local settings in setting.app

Sandisandidge answered 22/6, 2012 at 6:37 Comment(4)
@AlmasAdilbek could you mention how we can?! that's already mentioned in the UIDatePicker - Internationalization that changing the local would constraint the format.Orleans
@AhmadF Commin back from '16. Actually, look at the answer below ↓, initializing the NSLocale with the identifier, let's say kk_Cyrl_KZ will return 24-hour format.Revolute
@AlmasAdilbek which means that the format would be based on the assigned local. Check this answer.Orleans
@AlmasAdilbek Actually theres a work around for this. but, its only converting the value after it has been selected. not while selected. since the user device are in 12h when selecting time it preferable to used 12h as the user already familiar with the format. but can convert the value afterward by using dateformatter.Bing
M
14

Swift 4 version of ak2g's answer:

// myDatePicker is yourUIDatePicker Outlet
@IBOutlet weak var myDatePicker: UIDatePicker!

// For 24 Hrs 
myDatePicker.locale = Locale(identifier: "en_GB")

//For 12 Hrs
myDatePicker.locale = Locale(identifier: "en_US")
Mareld answered 6/12, 2017 at 9:20 Comment(0)
H
13

There is no need of any single Line of Code. We can set the locale from Story Board also, I have attached the screenshot.

enter image description here

And to do this programmatically we can also write the following code.

dateTimePicker.locale = Locale(identifier: "en_GB")
Hewie answered 22/7, 2017 at 7:11 Comment(1)
this no longer works in iOS16.5Felonry
E
5

Just use, For 24 hours format:

picker.locale = Locale.init(identifier: "en_gb")

For 12 hours format:

picker.locale = Locale.init(identifier: "en_us")

Why this works? Because en_gb is for British English, which prefers 24 hr format. And you guessed it right, en_us is for American English which prefers 12 hr format.

Eyeless answered 21/4, 2020 at 13:56 Comment(0)
B
4

Easily format the result instead

Since the device local are not in 24 hour. select time in 12h format has no problem. mainly this issue occurs when you are trying to submit a time to server. and the server requesting it on 24 hour format.

However, for user it is easier for user to used 12h format rather 24h format. but server are usually required time to be in 24h format.

Since you mention

not on device Settings

I can only assume you are trying to make this happen without interrupt user default setting on the Phone. But if your issue is with the view, then

myDatePicker.locale = NSLocale(localeIdentifier: "en_GB")

is the only way to do it.

What I did was not display it in 24h format but convert the result value to 24h format.

let dateFormatter = DateFormatter()
dateFormatter.dateFormat = "HH:mm"
print("\(pickerView.date)") // 10:30 PM
let dateSelect = dateFormatter.string(from: pickerView.date)//pickerView = to your UIDatePicker()
self.tfclosing.text = dateSelect // 22:30
Bing answered 9/4, 2018 at 8:43 Comment(0)
O
3

I've found an easier way, click on the UIDatePicker attributes inspector on storyboard, and set the locale to 'English (Europe)'. This takes away the AM/PM toggle and sets the time to 24hr format.

Obvert answered 28/9, 2014 at 2:34 Comment(1)
I wish there was more detail to this answer.Floridafloridia
C
2

Set your UIDatepicker to have a locale property so it doesn't default to the user's locale.

Here is the documentation for doing so, and make sure to set the country, not the language.

Edit: Actually, it looks like locale is depreciated in iOS 5.0. I guess Apple thought people shouldn't override it.

Canaanite answered 22/6, 2012 at 6:37 Comment(3)
Lawrence Wu can you please provide some code because I have already tried it it wont work.Inkster
I'm not sure anymore because it seems like the locale property is depreciated in the latest OS, but check out these similar questions: https://mcmap.net/q/452237/-24hr-amp-uidatepicker #2649219Canaanite
sorry the links are of no use.Can you suggest something else.Inkster
G
1

EDIT: While this seems like the right way to use this API, UIDatePicker does NOT respect the hourCycle property (yet?) and just goes off the Locale's region.
Not even DateFormatter respects it which is just baffling.

There is a new proper way to do this using iOS 16's new Locale.Components and the hourCycle property:

var components = Locale.Components(locale: .current)
components.hourCycle = use24h ? .zeroToTwentyThree : .oneToTwelve
let locale = Locale(components: components)
datePicker.locale = locale

This will not override the user's other locale settings like language etc.

Gimel answered 2/3, 2023 at 3:46 Comment(0)
H
0

i have an answer for this you can customize locale before setting it to datePicker.

locale consist of (language_region)

extension UIDatePicker {
internal func setLocale() {
    guard let countryCode = (Locale.current as NSLocale).object(forKey: .countryCode) as? String else {
        self.locale = LanguageManager.shared.appLocale
        return
    }
    self.locale = Locale(identifier: "\(LanguageManager.shared.appLocale.identifier)_\(countryCode)")
   }
}

where LanguageManager.shared.appLocale.identifier is selected language code for use (en, ar, fr, etc...)

and use it like

   let datePicker = UIDatePicker()
    datePicker.setLocale()

it will result locale for example like "fr_US" and US is the device country region so if user change app language to french for example it will use US region so it will be with 12h format although "fr" only will use 24 h format

Hypogastrium answered 16/2, 2021 at 13:42 Comment(0)
A
-1

If I understand you correctly you need to find out whether the device settings is has 12 hrs or 24 hrs format. Sorry if this solution is a little late.

 -(BOOL)returnDefaultDateFormat
   {
       NSString *formatStringForHours = [NSDateFormatter dateFormatFromTemplate:@"j"     options:0 locale:[NSLocale currentLocale]];
       NSRange containsA = [formatStringForHours rangeOfString:@"a"];
       BOOL hasAMPM = containsA.location != NSNotFound;
       return hasAMPM;
}
Aspinwall answered 12/12, 2013 at 6:51 Comment(0)

© 2022 - 2025 — McMap. All rights reserved.