views:

315

answers:

2

I thought I saw something answering this on SO recently but now I can't find it. Here is the code I am using now to determine if settings are for 24 hour time display. It works for me in the US, but I don't know if it will work in all locales. Is this sufficient or is there a better way to find out the current setting for this?

+(BOOL) use24HourClock
{
    BOOL using24HourClock = NO;

    NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];    
    [dateFormatter setLocale: [NSLocale currentLocale]];    
    [dateFormatter setDateStyle:kCFDateFormatterNoStyle];
    [dateFormatter setTimeStyle:kCFDateFormatterShortStyle];    
    // get date/time (1Jan2001 0000UTC)
    NSDate* midnight = [[NSDate alloc] initWithTimeIntervalSinceReferenceDate:0];   
    NSString* dateString = [dateFormatter stringFromDate: midnight];
    // dateString will either be "15:00" or "16:00" (depending on DST) or
    // it will be "4:00 PM" or "3:00 PM" (depending on DST)
    using24HourClock = ([dateString length] == 5);
    [midnight release];
    [dateFormatter release];    

    return using24HourClock;
}
+1  A: 

You can do it like this.

- (void)getTheTimeFormat{
 NSDateFormatter *dateForm = [[NSDateFormatter alloc] init];
 [dateForm setTimeStyle:NSDateFormatterMediumStyle];
 NSString *timeStr = [dateForm stringFromDate:[NSDate date]];
 NSArray *componentsArr = [timeStr componentsSeparatedByString:@" "];
 if(componentsArr.count == 1){
  NSLog(@"time format in 24 hours");
 }
 else{
  NSLog(@"time format in 12 hours");
 }
 [dateForm release];
}

Hope this helps.

Madhup
That effectively works by checking to see if there is a space in the time string (ie: "12:59:59 PM") but what if the time string doesn't have a space, such as: "12:59:59pm"? Not sure if that format is possible but I don't know.
progrmr
@kk6yb No If the time is in 24 hrs format it will always have space in between
Madhup
This doesn't always work because certain regional settings do not use a space, but instead use a period '.'. (Can't seem to find the example again as it's taking to long to go through them all!!)
Michael Waterfall
+1  A: 

I've just asked a similar question on here and I've managed to figure out a decent way of determining this with a little function I've added to a category of NSLocale. It appears to be pretty accurate and haven't found any problems with it while testing with several regions.

@implementation NSLocale (Misc)
- (BOOL)timeIs24HourFormat {
    NSDateFormatter *formatter = [[NSDateFormatter alloc] init];
    [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 is24Hour = amRange.location == NSNotFound && pmRange.location == NSNotFound;
    [formatter release];
    return is24Hour;
}
@end

Hope this helps!

Michael Waterfall