title: How to extract the date from Swift String as NSDate?
body: <p>My date is <code>&quot;2017-05-04 13:46:36.0&quot;</code>. How can I filter only the date from this?</p>
  <p>I have used this function:</p>
  <pre><code>func toDate(dateString : String, dateFormat : String = &quot;yyyy-MM-dd'T'HH:mm:ssX&quot;)-&gt; NSDate!{
    let dateFormatter = NSDateFormatter()
    dateFormatter.dateFormat = dateFormat
    dateFormatter.locale = NSLocale(localeIdentifier: &quot;en_US_POSIX&quot;)
    dateFormatter.timeZone = NSTimeZone(forSecondsFromGMT: 0)
    let convertedDate = dateFormatter.dateFromString(dateString)
    return convertedDate
  }
  
  let date = &quot;2017-05-04 13:46:36.0&quot;
  </code></pre>
  <p>now I have tried tried to set:</p>
  <pre><code>lbl.text = String.toDate(dateString: date, dateFormat: &quot;yyyy-MM-dd&quot;)
  </code></pre>
  <p>But it always returns nil and crashes the app? Why is this happening?</p>