Skip to content

Instantly share code, notes, and snippets.

@shahdhiren
Last active February 6, 2017 06:36
Show Gist options
  • Save shahdhiren/fafd05987cbbcce70e1336098ea234ff to your computer and use it in GitHub Desktop.
Save shahdhiren/fafd05987cbbcce70e1336098ea234ff to your computer and use it in GitHub Desktop.
Convert Date Format (From-To) Swift 3
func convertDateFormat(from: String, to: String, dateString: String?) -> String? {
let fromDateFormatter = DateFormatter()
fromDateFormatter.dateFormat = from
var formattedDateString: String? = nil
if dateString != nil {
let formattedDate = fromDateFormatter.date(from: dateString!)
if formattedDate != nil {
let toDateFormatter = DateFormatter()
toDateFormatter.dateFormat = to
formattedDateString = toDateFormatter.string(from: formattedDate!)
}
}
return formattedDateString
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment