Skip to content

Instantly share code, notes, and snippets.

@rafattouqir
Created May 20, 2019 08:10
Show Gist options
  • Save rafattouqir/fb5098f570ba68a93f95d97aacb6b983 to your computer and use it in GitHub Desktop.
Save rafattouqir/fb5098f570ba68a93f95d97aacb6b983 to your computer and use it in GitHub Desktop.
//call the method
let convertedDateString = self.dateFromString("2019-05-20", fromFormat: "yyyy-MM-dd", toFormat: "dd-MM-yyyy")
print(convertedDateString ?? "Failed to convert date")
func dateFromString(_ dateString:String, fromFormat:String, toFormat: String) -> String? {
let dateFormatter = DateFormatter()
dateFormatter.dateFormat = fromFormat
guard let fromDate = dateFormatter.date(from: dateString) else { return nil }
dateFormatter.dateFormat = toFormat
return dateFormatter.string(from: fromDate)
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment