Find if the user prefers 12 hours?

I have a drawRect that makes the timeline a bit like iCal. I use a for loop to write time along the scroll. I was wondering if A) theres a way to determine if the user has selected a 12 or 24-hour clock in the system settings, and B) if there is a more efficient way to change the timestamps, and then call the if request every passage of the 'for' loop . Greetings

+5
source share
3 answers
NSDate *today = [NSDate date];
NSString *formattedString = [NSDateFormatter localizedStringFromDate:today dateStyle: kCFDateFormatterNoStyle timeStyle: kCFDateFormatterShortStyle];

NSRange foundRange;
foundRange = [formattedString rangeOfString:"am" options:NSCaseInsensitiveSearch];
if(foundRange.location == NSNotFound) {
    foundRange = [formattedString rangeOfString:"pm" options:NSCaseInsensitiveSearch];
}

BOOL isAMPMSettingOn = (foundRange.location != NSNotFound);
+1
source

, "AM" "PM" . , keyur bhalodiya, , , AMSymbol PMSymbol NSDateFormatter.

-(BOOL)uses24hourTime
{
     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]];

     return (amRange.location == NSNotFound && pmRange.location == NSNotFound);
}
+6
NSDateFormatter *dateFormatter = [[[NSDateFormatter alloc] init] autorelease];
[dateFormatter setDateStyle:NSDateFormatterNoStyle];
[dateFormatter setTimeStyle:NSDateFormatterLongStyle];

if([[dateFormatter dateFormat] rangeOfString:@"a"].location != NSNotFound) {
    // user prefers 12 hour clock
} else {
    // user prefers 24 hour clock
}
+5
source