title: NSDateFormatter - does not respect 12/24 hour (am/pm) system setting in some circumstances
body: <p>I want to output a date (that has relative dates, e.g. Today, Yesterday etc.) and time (respecting the 12/24 hours setting on the iOS device) E.g.</p>
  
  <ul>
  <li><strong>Today 5:48 PM</strong> (when in 12 hour mode); and</li>
  <li><strong>Today 17:48</strong> (when in 24 hour mode)</li>
  </ul>
  
  <p>Configuring an NSFormatter like this (and using stringFromDate:) does not work:</p>
  
  <pre><code>NSDateFormatter* dateFormatter = [[NSDateFormatter alloc] init];
  [dateFormatter setLocale: [NSLocale autoupdatingCurrentLocale]];
  [dateFormatter setTimeStyle:NSDateFormatterShortStyle];  // doesn't respect am/pm setting on iOS device (grrr!)
  [dateFormatter setDateStyle:NSDateFormatterMediumStyle];
  [dateFormatter setDoesRelativeDateFormatting:YES]; // but does get relative dates
  </code></pre>
  
  <p>This gives you relative dates, but defaults to the 12/24 setting for the locale, e.g. 12 hours for US and 24 hours for UK. (Why Apple think UK people like 24 hours clocks, I don't know...)</p>
  
  <p>A work around that I've adopted uses two NSDateFormatters, one for the date and one for the time. It appears that when you configure an NSDateFormatter with a dateStyle of NSDateFormatterNoStyle and a timeStyle of NSDateFormatterShortStyle, then it does respects the 12/24 hour setting. (So it's just when you have BOTH date and time styles set that there is trouble.)</p>
  
  <p>I've included my work around below in case anyone else has a similar issue. Is there an easier way to do this? The work around seems somewhat awkward and it is not clear how confident I should be it will continue working in future revisions of iOS.</p>
  
  <p><strong>Work around</strong></p>
  
  <pre><code>- (NSString*) humanRelativeDateAndTimeAlt: (NSDate*) date;
  {
  
  NSDateFormatter* relativeDateFormatter = [[NSDateFormatter alloc] init];
  [relativeDateFormatter setLocale: [NSLocale autoupdatingCurrentLocale]];
  [relativeDateFormatter setTimeStyle:NSDateFormatterNoStyle]; // no time for this formatter
  [relativeDateFormatter setDateStyle:NSDateFormatterMediumStyle];
  [dateFormatter setDoesRelativeDateFormatting:YES]; // relative dates
  
  NSString* datePart = [[self relativeDateFormatter] stringFromDate:date];
  
  NSDateFormatter* timeDateFormatter = [[NSDateFormatter alloc] init];
  [timeDateFormatter setLocale:[NSLocale autoupdatingCurrentLocale]];
  [timeDateFormatter setDateStyle: NSDateFormatterNoStyle]; // no date for this formatter
  [timeDateFormatter setTimeStyle: NSDateFormatterShortStyle]; // does respect 12/24 hour setting
  
  NSString* timePart = [timeDateFormatter stringFromDate:date];
  
  int skipZero = 0;  // extra code to remove leading zero if present 
  if ([[timePart substringToIndex:1] isEqualToString:@"0"]) skipZero = 1;
  
  [relativeDateFormater release];
  [timeDateFormatter release];
  
  return [NSString stringWithFormat:@"%@ %@", datePart, [timePart substringFromIndex:skipZero]];
  }
  </code></pre>