31

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!

Gypsa
  • 11,230
  • 6
  • 44
  • 82

12 Answers12

38

Try this code to show 24 Hrs Format in DatePicker:

NSLocale *locale = [[NSLocale alloc] initWithLocaleIdentifier:@"NL"];
[self.datePicker setLocale:locale];
MikroDel
  • 6,705
  • 7
  • 39
  • 74
Avik
  • 433
  • 1
  • 4
  • 4
  • 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. – Ash May 28 '15 at 08:46
  • 3
    Wrong solution. It could result in time errors on DateTime picker since the picker is configured on the Netherlands locale! – Martin Jul 09 '15 at 10:32
  • Maybe it works in English, but setting fixed locale is bad idea – Honghao Z Jul 09 '15 at 18:40
  • @Martin how so? Can you provide an example? Could the errors be prevented somehow? – Radu Simionescu Nov 19 '15 at 12:56
  • @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 :) – Martin Nov 19 '15 at 17:59
  • ahah @RaduSimionescu, I just don't like using magic numbers (or magic strings) in my code :) – Martin Nov 20 '15 at 09:35
  • Unfortunately in iOS 14 it's not working. Changing the locale doesn't change anything in simulator. – heyfrank Oct 16 '20 at 10:08
  • 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. – heyfrank Oct 16 '20 at 11:28
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
Niall Kiddle
  • 1,477
  • 1
  • 16
  • 35
ak2g
  • 1,673
  • 15
  • 22
21

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

Omar Abdelhafith
  • 21,163
  • 5
  • 52
  • 56
  • 1
    @AlmasAdilbek could you mention how we can?! that's already mentioned in the [UIDatePicker](https://developer.apple.com/documentation/uikit/uidatepicker#//apple_ref/occ/instp/UIDatePicker/locale) - Internationalization that changing the local would constraint the format. – Ahmad F Nov 01 '17 at 08:58
  • @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. – Almas Adilbek Nov 01 '17 at 10:24
  • @AlmasAdilbek which means that the format would be based on the assigned local. Check [this answer](https://stackoverflow.com/questions/47049344/localizing-customizing-the-date-format-of-a-uidatepicker/47051124#47051124). – Ahmad F Nov 01 '17 at 10:33
  • @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. – Muhammad Asyraf Apr 09 '18 at 08:50
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")
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")
Jaydeep Vyas
  • 4,411
  • 1
  • 18
  • 44
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.

Bibek
  • 3,689
  • 3
  • 19
  • 28
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
Pang
  • 9,564
  • 146
  • 81
  • 122
Muhammad Asyraf
  • 1,748
  • 15
  • 20
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.

naveedez
  • 46
  • 4
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.

Community
  • 1
  • 1
Lawrence Wu
  • 212
  • 1
  • 2
  • 12
  • Lawrence Wu can you please provide some code because I have already tried it it wont work. – Gypsa Jun 22 '12 at 06:42
  • I'm not sure anymore because it seems like the locale property is depreciated in the latest OS, but check out these similar questions: http://stackoverflow.com/questions/6197641/24hr-uidatepicker http://stackoverflow.com/questions/2648719/uidatepicker-locale-does-nothing – Lawrence Wu Jun 22 '12 at 06:44
  • sorry the links are of no use.Can you suggest something else. – Gypsa Jun 22 '12 at 07:15
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

0

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.

YourMJK
  • 1,429
  • 1
  • 11
  • 22
-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;
}
GhostCode
  • 452
  • 6
  • 17