title: iOS : Switching between 12/24 hour times from strings
body: <p>Interesting issue thats caught me out.</p>
  
  <p>I receive string times from a server to a device. which I then convert in to a NSDate.
  When the device was set to displaying 24hour times, life was good.</p>
  
  <p>Now I am testing it on a device set to 12hour times. everything has stopped working.  Dates are coming back as null</p>
  
  <p>I first had</p>
  
  <pre><code> NSDateFormatter *dateFormat = [[NSDateFormatter alloc] init];
   [dateFormat setDateFormat:@"HH:mm"];
   self.startTime = [dateFormat dateFromString:(NSString *)self.startTime];
  </code></pre>
  
  <p>Worked perfectly for devices showing 24hour dates but not 12hour.</p>
  
  <p>I then tried </p>
  
  <pre><code> NSDateFormatter *dateFormat = [[NSDateFormatter alloc] init];
   [dateFormat setDateFormat:@"hh:mm"];
   self.startTime = [dateFormat dateFromString:(NSString *)self.startTime];
  </code></pre>
  
  <p>This works fine up until 12 noon, then all dates are returned as null</p>
  
  <p><strong>Update</strong></p>
  
  <p>I have also tried adding "a" but this still results in returning null</p>
  
  <pre><code> if (startDate == nil)
   {
        NSDateFormatter *dateFormat = [[NSDateFormatter alloc] init];
        [dateFormat setDateFormat:@"hh:mm a"];
        startDate = [dateFormat dateFromString:(NSString *)self.startTime];
   }
  </code></pre>
  
  <p><strong>Update 2</strong></p>
  
  <p>Adding local, adding :ss adding a all still do not work</p>
  
  <pre><code>NSDateFormatter *dateFormat = [[NSDateFormatter alloc] init];
  NSLocale *twelveHourLocale = [[NSLocale alloc] initWithLocaleIdentifier:@"en_US_POSIX"];
  dateFormat.locale = twelveHourLocale;
  [dateFormat setDateFormat:@"hh:mm a"];
  startDate = [dateFormat dateFromString:(NSString *)self.startTime];
  </code></pre>