title: Convert ISO8601 DateTime String to Date in Swift
body: <p>I am attempting to convert a string in ISO8601 format that includes the timezone offset (in this case, Eastern Time Zone) into a date but the conversion returns a nil value:</p>
  <pre class="lang-swift prettyprint-override"><code>func utcStringToLocalDate(dateString: String) -&gt; Date? {        
      let dateFormatter = DateFormatter()
      dateFormatter.locale = Locale(identifier: &quot;en_US_POSIX&quot;)
      dateFormatter.dateFormat = &quot;yyyy-MM-dd'T'HH:mm:ssZZZZZ&quot;
  
      let convertedDate = dateFormatter.date(from: dateString)
                  
      return convertedDate
  }   
  </code></pre>
  <p>calling this function like so:</p>
  <pre class="lang-swift prettyprint-override"><code>utcStringToLocalDate(dateString: &quot;2020-07-29T12:00:00-05:00&quot;)
  </code></pre>
  <p>will return a nil value</p>
  <p>I also tried</p>
  <pre class="lang-swift prettyprint-override"><code>func utcStringToLocalDate(dateString: String) -&gt; Date? {
      let iso8601Formatter = ISO8601DateFormatter()
      iso8601Formatter.formatOptions = [.withInternetDateTime,
                                        .withDashSeparatorInDate,
                                        .withFullDate,
                                        .withColonSeparatorInTimeZone]
  
      let convertedDate = iso8601Formatter.date(from: dateString)
    }
  </code></pre>
  <p>but there was no joy either, it also returned nil</p>
  <p>What is the proper approach or format that will return an ISO 8601 date such as <em>2020-07-29T12:00:00-05:00</em> or <em>2020-07-29T12:00:00-0500</em>?</p>