ios 根据不在设备设置上的应用程序设置,在 UIDatepicker 中以 12 和 24 小时格式显示时间

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/11151187/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-30 18:53:56  来源:igfitidea点击:

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

iphoneobjective-ciosuidatepickertime-format

提问by Gypsa

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 UIDatePickertime is not getting changed.It's time is depending on device time settings format.Is it possible to show time in UIDatePickeraccording 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 UIDatePickerbased on my app settings.

我的问题是我的应用程序中有一个开关,可以切换 12 小时和 24 小时时间格式。我根据那个显示我的标签时间,但UIDatePicker时间没有改变。时间取决于设备时间设置格式。是可以UIDatePicker根据我的应用程序设置显示时间,这可能与设备时间设置不同。在某处我读到 UIDatePicker 仅尊重设备设置,但我仍然想问是否可以UIDatePicker根据我的应用程序设置以 12 和 24 小时格式显示时间。

Please help me. Any suggestions would be highly appreciated!

请帮我。任何建议将不胜感激!

采纳答案by Omar Abdelhafith

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

由于UIDatePicker您不能将时间格式更改为 24 或 12,这取决于 setting.app 中的用户本地设置

回答by Avik

Try this code to show 24 Hrs Format in DatePicker:

试试这个代码来显示 24 小时格式DatePicker

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

回答by 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

回答by Mario Steingruber

Swift 4version of ak2g's answer:

Swift 4版本的 ak2g 答案:

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

回答by Jaydeep Vyas

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

不需要任何一行代码。我们也可以从 Story Board 设置语言环境,我附上了截图。

enter image description here

在此处输入图片说明

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

为了以编程方式执行此操作,我们还可以编写以下代码。

dateTimePicker.locale = Locale(identifier: "en_GB")

回答by naveedez

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.

我找到了一种更简单的方法,单击情节提要上的 UIDatePicker 属性检查器,并将语言环境设置为“英语(欧洲)”。这将取消 AM/PM 切换并将时间设置为 24 小时格式。

回答by 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.

由于设备本地不在 24 小时内。选择12h格式的时间没有问题。这个问题主要发生在您尝试向服务器提交时间时。和服务器以 24 小时格式请求它。

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.

但是,对于用户来说,使用 12h 格式而不是 24h 格式更容易。但服务器通常需要时间为 24 小时格式。

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.

我所做的不是以 24 小时格式显示它,而是将结果值转换为 24 小时格式。

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

回答by Lawrence Wu

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

将您的 UIDatepicker 设置为具有区域设置属性,因此它不会默认为用户的区域设置。

Here is the documentationfor 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.

编辑:实际上,iOS 5.0 中的语言环境似乎已贬值。我猜苹果认为人们不应该覆盖它。

回答by Dari

Just use, For 24 hours format:

只需使用,对于 24 小时格式:

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

For 12 hours format:

对于 12 小时格式:

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.

为什么这有效?因为 en_gb 用于英式英语,它更喜欢 24 小时格式。你猜对了, en_us 是美国英语,它更喜欢 12 小时格式。

回答by GhostCode

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.

如果我理解正确,您需要确定设备设置是 12 小时还是 24 小时格式。对不起,如果这个解决方案有点晚了。

 -(BOOL)returnDefaultDateFormat
   {
       NSString *formatStringForHours = [NSDateFormatter dateFormatFromTemplate:@"j"     options:0 locale:[NSLocale currentLocale]];
       NSRange containsA = [formatStringForHours rangeOfString:@"a"];
       BOOL hasAMPM = containsA.location != NSNotFound;
       return hasAMPM;
}