Skip to content

Instantly share code, notes, and snippets.

@johncodeos
Last active January 19, 2022 09:02
Show Gist options
  • Save johncodeos/6753ece609b0c96b52f5d7b21c18f250 to your computer and use it in GitHub Desktop.
Save johncodeos/6753ece609b0c96b52f5d7b21c18f250 to your computer and use it in GitHub Desktop.
Convert String date to Date
extension String {
func iso8601Value() -> Date? {
let theDate = self
let formatter = DateFormatter()
formatter.dateFormat = "yyyy-MM-dd'T'HH:mm:ssZZZZ"
let date = formatter.date(from: theDate as String)
return date
}
}
// How to use it:
let time = "2022-01-15T10:16:12-04:00".iso8601Value() ?? Date()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment