How can I determine if iPhone is set for 12 hour or 24 hour time display?

后端 未结 5 1116
陌清茗
陌清茗 2020-12-01 04:13

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

5条回答
  •  醉酒成梦
    2020-12-01 04:51

    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 setLocale:self];
        [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!

提交回复
热议问题