0

I am trying to convert the String object into Date Object as follows, but getting a nil. I am wondering what I am missing in my current implementation.

let dateInString  = "2021-02-27T17:23:37.771791Z"
// the following returns nil
let dateObj =  dateInString.toDate()

extension String {

    func toDate(withFormat format: String = "yyyy-MM-dd'T'HH:mm:ss.SSSSSSZ")-> Date?{

      let dateFormatter = DateFormatter()
      dateFormatter.locale = Locale(identifier: "en_US_POSIX")
      dateFormatter.dateFormat = format
      let date = dateFormatter.date(from: self)
      return date
    }
}
konya
  • 131
  • 2
  • 8

0 Answers0