检测时间格式是12小时还是24小时格式 [英] Detect if time format is in 12hr or 24hr format

查看:747
本文介绍了检测时间格式是12小时还是24小时格式的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

有什么方法来检测应用程序的当前设备是否使用12h我们的24h格式,所以我可以使用一个NSDateFormatter为12h和一个24h取决于用户的语言/爱好设置?就像UIDatePicker检测和显示AM / PM选择器如果是12h格式。

Is there any way to detect if the current device of the app uses 12h our 24h format, so that I can use one NSDateFormatter for 12h and one for 24h depending on the users language/loaction setting? Just Like the UIDatePicker detects and shows the AM/PM picker if it is 12h format.

推荐答案

简单。我只是添加这个代码到 viewDidLoad

I figured it out, its pretty easy. I just added this code to viewDidLoad :

NSDateFormatter *formatter = [[NSDateFormatter alloc] init];
[formatter setLocale:[NSLocale currentLocale]];
[formatter setDateStyle:NSDateFormatterNoStyle];
[formatter setTimeStyle:NSDateFormatterShortStyle];
NSString *dateString = [formatter stringFromDate:[NSDate date]];
NSRange amRange = [dateString rangeOfString:[formatter AMSymbol]];
NSRange pmRange = [dateString rangeOfString:[formatter PMSymbol]];
BOOL is24h = (amRange.location == NSNotFound && pmRange.location == NSNotFound);
[formatter release];
NSLog(@"%@\n",(is24h ? @"YES" : @"NO"));

并完全返回 YES NO ,具体取决于区域设置。

And it perfectly returns YES or NO depending on the locale.

这篇关于检测时间格式是12小时还是24小时格式的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆