Skip to content

Instantly share code, notes, and snippets.

@unferna
Created December 7, 2019 21:25
Show Gist options
  • Save unferna/c0be15f90e91eebe2f9c27001010c84e to your computer and use it in GitHub Desktop.
Save unferna/c0be15f90e91eebe2f9c27001010c84e to your computer and use it in GitHub Desktop.
[Convert String to Date] #Swift #String #Date
extension String {
/**
Convert a `String` to `Date`
- parameters:
- format: Input format to work of String
*/
func asDate(format: String) -> Date? {
let df = DateFormatter()
df.dateFormat = format
df.timeZone = TimeZone.current
return df.date(from: self)
}
}
let testDate = "2019-08-14".asDate(format: "yyyy-MM-dd")
print(testDate)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment