在ios区域是“英国”时如何获取系统时间格式?

前端之家收集整理的这篇文章主要介绍了在ios区域是“英国”时如何获取系统时间格式?前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。
我在ios中使用以下代码获取系统时间格式.当我目前的地区设置为“美国”时,工作正常,但当我将该地区从“美国”改为“英国”时,总是给予12小时的格式.
#pragma mark
#pragma mark - get system time
-(BOOL)getSystemTimeFormat
{
    NSString *strDateFormate = @"hh a";
    NSString *dateFormat = [NSDateFormatter dateFormatFromTemplate:strDateFormate options:0 locale:[NSLocale currentLocale]];
    if ([dateFormat rangeOfString:@"h"].location != NSNotFound)
    {
        [kNSUserDefaults setBool:YES forKey:@"TimeFormat"];
        [kNSUserDefaults synchronize];
        return YES;
    }
    else
    {
        [kNSUserDefaults setBool:NO forKey:@"TimeFormat"];
        [kNSUserDefaults synchronize];
        return NO;
    }

}

“美国”地区的日志::

2015-01-27 11:32:16.090 [350:60b] 12 Formate :: 0
2015-01-27 11:32:16.585 [350:60b] Connect
2015-01-27 11:32:16.591 [350:60b] Loction update…
2015-01-27 11:32:16.604 [350:60b] Loction update…
2015-01-27 11:32:16.622 [350:60b] Loction update…

日本的“英国”地区::

2015-01-27 11:33:35.785 [364:60b] 12 Formate :: 1
2015-01-27 11:33:36.777 [364:60b] Connect
2015-01-27 11:33:36.780 [364:60b] Loction update…
2015-01-27 11:33:36.806 [364:60b] Loction update…
2015-01-27 11:33:36.832 [364:60b] Loction update…

解决方法

经过很多关于这个话题的研究,我终于找到了这个问题的解决方案.

这使用一个特殊的日期模板字符串“j”.根据ICU Spec,“j”…

Requests the preferred hour format for the locale (h,H,K,or k),as determined by whether h,or k is used in the standard short time format for the locale. In the implementation of such an API,‘j’ must be replaced by h,or k before beginning a match against availableFormats data. Note that use of ‘j’ in a skeleton passed to an API is the only way to have a skeleton request a locale’s preferred time cycle type (12-hour or 24-hour).
That last sentence is important. It “is the only way to have a skeleton request a locale’s preferred time cycle type”. Since NSDateFormatter and NSCalendar are built on the ICU library,the same holds true here.

所以我改变了我的代码中的templet值

#pragma mark
#pragma mark - get system time
-(BOOL)getSystemTimeFormat
{
    NSString *strDateFormate = @"j";
    NSString *dateFormat = [NSDateFormatter dateFormatFromTemplate:strDateFormate options:0 locale:[NSLocale currentLocale]];
    if ([dateFormat rangeOfString:@"a"].location != NSNotFound)
    {
        [kNSUserDefaults setBool:YES forKey:@"TimeFormat"];
        [kNSUserDefaults synchronize];
        return YES;
    }
    else
    {
        [kNSUserDefaults setBool:NO forKey:@"TimeFormat"];
        [kNSUserDefaults synchronize];
        return NO;
    }

}

我从以下链接How can I determine if iPhone is set for 12 hour or 24 hour time display?找到了这个解决方案.

原文链接:https://www.f2er.com/iOS/337088.html

猜你在找的iOS相关文章