chirag shah chirag shah - 1 year ago 150
iOS Question

How to detect the system time format change in ios?

I want to detect the time format change done in the system setting. I used following code but it always give me the time old format. How can i get new time format?

I used following methods...

#pragma mark
#pragma mark - application change time format
-(void)applicationSignificantTimeChange:(UIApplication *)application
NSDateFormatter *formatter = [[NSDateFormatter alloc] init];
[formatter setLocale:[NSLocale currentLocale]];
[formatter setDateStyle:NSDateFormatterNoStyle];
[formatter setTimeStyle:NSDateFormatterShortStyle];
[formatter setTimeZone:[NSTimeZone localTimeZone]];
NSString *dateString = [formatter stringFromDate:[NSDate date]];
NSLog(@"dataString ::%@",dateString);
NSRange amRange = [dateString rangeOfString:[formatter AMSymbol]];
NSRange pmRange = [dateString rangeOfString:[formatter PMSymbol]];
is12Hour = (amRange.length > 0 || pmRange.length > 0);

Answer Source

I'm not sure why you expect that the dateFormat changes if the date changes significantly.
The significant time change event is triggered when the time (i.e. [NSDate date]) changes. For example if a new day starts, if the user changes timezone or if daylight-saving starts or ends.
But those events don't change the date format.

I think you want to monitor locale changes. There is a notification for that: NSCurrentLocaleDidChangeNotification.

Something like this should work:

j is a template that will be replaced with h a (12 hour format) or H (24 hour format) by the date template method

id localeDidChangeNotification = [[NSNotificationCenter defaultCenter] addObserverForName:NSCurrentLocaleDidChangeNotification object:nil queue:[NSOperationQueue mainQueue] usingBlock:^(NSNotification *note) {
    NSString *dateFormat = [NSDateFormatter dateFormatFromTemplate:@"j" options:0 locale:[NSLocale currentLocale]];
    if ([dateFormat rangeOfString:@"h"].location != NSNotFound) {
        // 12 hour
    else {
        // 24 hour

// Don't forget to remove the notification in the appropriate place
// [[NSNotificationCenter defaultCenter] removeObserver:localeDidChangeNotification];
Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download