title: How to convert string to NSDate in Swift?
body: <p>How can I convert a string like <code>05/18/2017</code> this to <code>NSDate</code>?</p>
  
  <p>I am trying to convert this string to <code>NSDate</code> so that I can extract the day as <strong>18</strong> and month as <strong>05</strong> and year as <strong>2017</strong>. How can I convert this or can extract those values from strings.</p>
  
  <pre><code>func toDate(dateString : String, dateFormat : String = "yyyy-MM-dd'T'HH:mm:ssX")-&gt; NSDate!{
    let dateFormatter = NSDateFormatter()
    dateFormatter.dateFormat = dateFormat
    dateFormatter.locale = NSLocale(localeIdentifier: "en_US_POSIX")
    dateFormatter.timeZone = NSTimeZone(forSecondsFromGMT: 0)
    let convertedDate = dateFormatter.dateFromString(dateString)
    return convertedDate
  }
  </code></pre>
  
  <p>Now in code below <code>selectedValue</code> is <code>05/18/2017</code> but <code>NSDateFromString</code> always shows nil value:</p>
  
  <pre><code>let nsdateFromString = String.toDate(selectedValue)
  </code></pre>
  
  <p>What can I do in this case?</p>