Show Time in 12 and 24 hour format in UIDatepicker on the basis of app settings not on device Settings
Solution 1
For UIDatePicker
you cannot change the time format to 24 or 12, it depends on the user local settings in setting.app
Solution 2
Try this code to show 24 Hrs Format in DatePicker
:
NSLocale *locale = [[NSLocale alloc] initWithLocaleIdentifier:@"NL"];
[self.datePicker setLocale:locale];
Solution 3
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
Solution 4
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")
Solution 5
There is no need of any single Line of Code. We can set the locale from Story Board also, I have attached the screenshot.
And to do this programmatically we can also write the following code.
dateTimePicker.locale = Locale(identifier: "en_GB")
Comments
-
Gypsa about 3 years
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 inUIDatePicker
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 inUIDatePicker
based on my app settings.Please help me. Any suggestions would be highly appreciated!