Showing posts with label AM. Show all posts
Showing posts with label AM. Show all posts

Monday, November 20, 2017

iOS Checking device is in 24 hour format or not

We need to check the device time format and display time stuff accordingly in the app.

12-hour format

Added to Shopping cart at 01:00 PM

24-hour format 

Added to Shopping cart at 13:00

Objective C



  + (BOOL)isDeviceIn24HoursFormat {

     NSString *dateFormat = [NSDateFormatter 
           dateFormatFromTemplate:@"j" options:0 locale:     
                              [NSLocale currentLocale]];

     if ([dateFormat rangeOfString:@"a"].location == 
                                                           NSNotFound) {
        return YES;
     }

     return NO;

  }




Swift


  func isDeviceIn24HoursFormat() -> Bool {

        let dateFormat: String? = 
                    DateFormatter.dateFormat(fromTemplate: 
                       "j", options: 0, locale: NSLocale.current) 

        if ((dateFormat as NSString?)?.range(of: "a"))?.location 
                                                            == NSNotFound
        {
            return true
        }

        return false
    }
 }



12-hour format    dateFormat  =  "h a"
24-hour format    dateFormat  =  "HH"


Hope this post is useful. Feel free to comment incase of any queries.