title: How to parse ISO 8601 using NSDateFormatter with optional milliseconds part
body: <p>I'm trying to use an NSDateFormatter to parse dates that are in either of these formats</p>
  
  <pre><code>@"2013-02-01T14:21:00"
  </code></pre>
  
  <p>or</p>
  
  <pre><code>@"2013-02-01T14:21:56.345"
  </code></pre>
  
  <p>Currently I am using the below method to parse the string and return a date:</p>
  
  <pre><code>NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];
  [dateFormatter setTimeZone:[NSTimeZone timeZoneWithName:@"UTC"]];
  [dateFormatter setDateFormat:@"yyyy-MM-dd'T'HH:mm:ss"];
  
  [dateFormatter setLocale:[[NSLocale alloc] initWithLocaleIdentifier:@"en_US_POSIX"]];
  NSDate *date = [dateFormatter dateFromString:dateToFormat];
  </code></pre>
  
  <p>This works fine for the first style of date but returns nil for a string that includes the milliseconds part.</p>
  
  <p>I suppose I could test for the existence of the milliseconds and strip them but I was wondering if I could change the date format to treat the .SSS as optional?</p>
  
  <p>Thanks for your help</p>