title: Swift DateFormatter Optional Milliseconds
body: <p>I have the following code to parse an ISO8601 date.</p>
  
  <pre><code>let dateFormatter = DateFormatter()
  dateFormatter.dateFormat = "yyyy-MM-dd'T'HH:mm:ss.SSSZZZZZ"
  </code></pre>
  
  <p>Problem is sometimes the date is in a format like <code>2018-01-21T20:11:20.057Z</code>, and other times it's in a format like <code>2018-01-21T20:11:20Z</code>.</p>
  
  <p>So basically part of the time it has the <code>.SSS</code> millisecond part, and other times it does not.</p>
  
  <p>How can I setup the date formatter to make that part optional?</p>
  
  <p><strong>Edit</strong></p>
  
  <p>I forgot to mention a few details tho in my question I just realized. So I'm using the JSON Codable feature in Swift 4. So it just throws an error if it fails.</p>
  
  <p>So I basically have the following code.</p>
  
  <pre><code>let dateFormatter = DateFormatter()
  dateFormatter.dateFormat = "yyyy-MM-dd'T'HH:mm:ss.SSSZZZZZ"
  let decoder = JSONDecoder()
  decoder.dateDecodingStrategy = .formatted(isoMilisecondDateFormatter())
  
  return try decoder.decode([Person].self, from: _people)
  </code></pre>
  
  <p>An example JSON object for <code>_people</code> is the following.</p>
  
  <pre><code>[
      {
          "name": "Bob",
          "born": "2018-01-21T20:11:20.057Z"
      },
      {
          "name": "Matt",
          "born": "2018-01-21T20:11:20Z"
      }
  ]
  </code></pre>
  
  <p>The API I'm working with is pretty inconsistent so I have to deal with multiple types of data.</p>