14

I am using the following code for get system time format in ios. And it work fine when my current region set with "United State" but when I change the region from "United State" to "United Kingdom" it always give 12 hour formate.

#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;
    }

}

Log of the "United State" region::

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...

Log of the "United Kingdom" region::

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...

Viral Savaj
  • 3,379
  • 1
  • 26
  • 39
Chirag Shah
  • 3,034
  • 1
  • 30
  • 61
  • There are numerous questions in relation to this. What it boils down to is the [gotcha listed about *The returned string may not contain exactly those components given in template, but may—for example—have locale-specific adjustments applied.*](https://developer.apple.com/library/mac/documentation/Cocoa/Reference/Foundation/Classes/NSDateFormatter_Class/#//apple_ref/occ/clm/NSDateFormatter/dateFormatFromTemplate:options:locale:). If you want to guarantee the correct result for the formatter use the locale `en_US_POSIX` – Anya Shenanigans Jan 27 '15 at 16:49
  • @Petesh Using `en_US_POSIX` defeats the purpose of using the `dateFormatFromTemplate` method. – rmaddy Jan 27 '15 at 16:50
  • I am well aware of that. The issue is that locale specific adjustments include altering the format that you ask for i.e. you ending up with 12 hour time rather than the asked 24 hour time format. Apple believe that deferring to the user rather than the programmer is the way to go with this. – Anya Shenanigans Jan 27 '15 at 16:52
  • 1
    @Petesh In this case, i don't think so. I just tested this code. Regardless of the 24-hour time setting, I get `h a` as the resulting format for both locales. My results don't seem correct and they don't match what the OP is reporting. – rmaddy Jan 27 '15 at 16:57
  • In this case it's the '24-Hour Time' in the `Date & Time` preferences that overrides this. – Anya Shenanigans Jan 27 '15 at 17:06
  • @Petesh i try using en_US_POSIX but still not display me the right format – Chirag Shah Jan 28 '15 at 05:28
  • 1
    Hi @Petesh i am also face this issue any idea about that.How can i solved this issue ? Can you put some code snippet or any useful link for that issue . Thanx . – Jay Mehta Jan 28 '15 at 05:39
  • 2
    @chiragshah, If you always want that 24 Hour time format then why are you using NSString *strDateFormate = @"hh a"; instead I think you should use NSString *strDateFormate = @"HH a";. Hope this might help. ref -> http://unicode.org/reports/tr35/tr35-6.html#Date_Format_Patterns – Chahal May 03 '15 at 10:14

2 Answers2

8

After lot of r&d on this topic finally i find the solution for this issue.

This uses a special date template string called "j". According to the 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.

So i have change the templet value in my code

#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;
    }

}

I found this solution from following link How can I determine if iPhone is set for 12 hour or 24 hour time display?.

Community
  • 1
  • 1
Chirag Shah
  • 3,034
  • 1
  • 30
  • 61
4

You're basically trying to detect if 12h or 24h format is being used, correct? If so, try:

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

Source: Detect if time format is in 12hr or 24hr format

Community
  • 1
  • 1
Denis de Bernardy
  • 75,850
  • 13
  • 131
  • 154