 
 private let UTCFormat = "yyyy-MM-dd'T'HH:mm:ss.SSSZ"
 
-private let ISO8601DateFormatter: NSDateFormatter = {
-    let dateFormatter = NSDateFormatter()
-    let enUSPOSIXLocale = NSLocale(localeIdentifier: "en_US_POSIX")
+private let ISO8601DateFormatter: DateFormatter = {
+    let dateFormatter = DateFormatter()
+    let enUSPOSIXLocale = Locale(localeIdentifier: "en_US_POSIX")
     dateFormatter.locale = enUSPOSIXLocale
     dateFormatter.dateFormat = "yyyy-MM-dd'T'HH:mm:ss.SSSZ"
     return dateFormatter


import Foundation

internal let calendar = Calendar.current
internal let dateFormatter = DateFormatter()

internal let ISO8601DateFormatter: DateFormatter = {
    let dateFormatter = DateFormatter()
    let enUSPOSIXLocale = Locale(localeIdentifier: "en_US_POSIX")
    dateFormatter.locale = enUSPOSIXLocale
    dateFormatter.dateFormat = "yyyy-MM-dd'T'HH:mm:ss.SSSZ"
    return dateFormatter
}()

internal extension NSDate {
    

    
    class func dateFromString(_ string: AnyObject?) -> NSDate? {
        guard let dateString = string as? String else { return nil }
        
        let count = dateString.characters.count
        if count <= 10 {
            ISO8601DateFormatter.dateFormat = "yyyy-MM-dd"
        }
        else {
            ISO8601DateFormatter.dateFormat = UTCFormat
        }
        
        return ISO8601DateFormatter.date(from: dateString)
    }
}
