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

随声附和 提交于 2019-11-27 20:06:47

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

Avik

Try this code to show 24 Hrs Format in DatePicker:

NSLocale *locale = [[NSLocale alloc] initWithLocaleIdentifier:@"NL"];
[self.datePicker setLocale:locale];
ak2g

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

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

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

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.

Lawrence Wu

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.

Muhammad Asyraf

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

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;
}
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!