ios region "United Kingdom" 如何获取系统时间格式?
How to get system time format when region is "United Kingdom" in ios?
我使用以下代码在 ios 中获取系统时间格式。当我当前的区域设置为 "United State" 时它工作正常但是当我将区域从 "United State" 更改为 "United Kingdom" 时它总是给出 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;
}
}
"United State" 区域的日志::
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...
"United Kingdom" 区域的日志::
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...
您基本上是在尝试检测使用的是 12 小时格式还是 24 小时格式,对吗?如果是这样,请尝试:
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"));
来源:Detect if time format is in 12hr or 24hr format
经过对这个主题的大量研究和开发,我终于找到了这个问题的解决方案。
这使用了一个名为 "j" 的特殊日期模板字符串。根据 ICU Spec, "j"...
Requests the preferred hour format for the locale (h, H, K, or k), as determined by whether h, H, K, 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, H, K, 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.
所以我更改了代码中的模板值
#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;
}
}
我从以下 link How can I determine if iPhone is set for 12 hour or 24 hour time display?.
中找到了这个解决方案
我使用以下代码在 ios 中获取系统时间格式。当我当前的区域设置为 "United State" 时它工作正常但是当我将区域从 "United State" 更改为 "United Kingdom" 时它总是给出 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;
}
}
"United State" 区域的日志::
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...
"United Kingdom" 区域的日志::
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...
您基本上是在尝试检测使用的是 12 小时格式还是 24 小时格式,对吗?如果是这样,请尝试:
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"));
来源:Detect if time format is in 12hr or 24hr format
经过对这个主题的大量研究和开发,我终于找到了这个问题的解决方案。
这使用了一个名为 "j" 的特殊日期模板字符串。根据 ICU Spec, "j"...
Requests the preferred hour format for the locale (h, H, K, or k), as determined by whether h, H, K, 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, H, K, 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.
所以我更改了代码中的模板值
#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;
}
}
我从以下 link How can I determine if iPhone is set for 12 hour or 24 hour time display?.
中找到了这个解决方案