title: Swift dateFormat for variable dateType strings
body: <p>I am trying to parse some json which is the result of an oData connection and I am getting the following response from the server:</p>
  
  <pre><code>"Task_ID":24,"Equipment_ID":3,"Owner_ID":2,"Priority":5,"Date_Due":
  "2015-04-08T19:37:56.913","Time_Complete":"2015-04-09T19:37:56","Task_Description"
  </code></pre>
  
  <p>I am actually interested at the two different date fields I am receiving:</p>
  
  <pre><code>"Date_Due":"2015-04-08T19:37:56.913" 
  </code></pre>
  
  <p>and</p>
  
  <pre><code>"Time_Complete":"2015-04-09T19:37:56"
  </code></pre>
  
  <p>As we can see, one has the millisecond timestamp and the other one does not.</p>
  
  <p>By looking at the DB, this happens because the millisecond  is actually .000 in the database (MS SQL Server) and for whatever reason, the result I receive in my json has this part truncated.</p>
  
  <p>I am not interested in the milliseconds but I would like to have a dateFormat function that can handle both scenarios.</p>
  
  <p>Now I have an obvious answer which is (pre)parse every date field, remove the milliseconds if they are there and then use the following code to format them:</p>
  
  <pre><code>let SQLDateFormatter: NSDateFormatter = {
      let formatter = NSDateFormatter()
      formatter.dateFormat = "yyyy-MM-dd'T'HH:mm:ss"
      formatter.locale = NSLocale(localeIdentifier: "en_US_POSIX")
      formatter.timeZone = NSTimeZone(forSecondsFromGMT: 0)
      return formatter
  }()
  </code></pre>
  
  <p>However I would like to know if we can build a formatter that can solve this issue without pre-parsing, something that would be able to take both:</p>
  
  <pre><code>formatter.dateFormat = "yyyy-MM-dd'T'HH:mm:ss"
  </code></pre>
  
  <p>and</p>
  
  <pre><code>formatter.dateFormat = "yyyy-MM-dd'T'HH:mm:ss.SSS"
  </code></pre>
  
  <p>and automatically do the formatting properly.</p>
  
  <p>I have been checking for this issue but could not find anything out there, thanks in advance...</p>