Andrew
Andrew

Reputation: 16051

Check for 24 hour mode or AM/PM mode, for use with UIDatePicker

I have a border which wraps around UIDatePicker, to hide the blue surrounding it with a custom view.. I'm aware that in time mode, it's not possible to set it as specifically an AM/PM datepicker, or as a 24 hour datepicker. I'm wondering how I would go about finding out what it's currently set at, though, so I can adjust my border around that, as they're both different sizes.

Upvotes: 0

Views: 720

Answers (1)

It's pretty easy, you should check if AMSymbol and PMSymbol is "NSNotFound" in myDateString:

+(BOOL) is24h {
  NSDateFormatter *myFormatter = [[NSDateFormatter alloc] init];
  [myFormatter setLocale:[NSLocale currentLocale]]; 
  [myFormatter setDateStyle:NSDateFormatterNoStyle]; 
  [myFormatter setTimeStyle:NSDateFormatterShortStyle]; 

  NSString *myDateString = [myFormatter stringFromDate:[NSDate date]]; 
  NSRange myAmRange = [myDateString rangeOfString:[myFormatter AMSymbol]]; 
  NSRange myPmRange = [myDateString rangeOfString:[myFormatter PMSymbol]]; 

  return (myAmRange.location == NSNotFound && myPmRange.location == NSNotFound);
}

Upvotes: 4

Related Questions