I\'m trying to format a date from a string into another format.
For example: 2012-05-29 23:55:52 into 29/05 *newline* 2010.
I just
For those who prefer to use extension.
extension String {
func formattedDate(inputFormat: String, outputFormat: String) -> String {
let inputFormatter = DateFormatter()
inputFormatter.dateFormat = inputFormat
if let date = inputFormatter.date(from: self) {
let outputFormatter = DateFormatter()
outputFormatter.dateFormat = outputFormat
return outputFormatter.string(from: date)
} else {
return self // If the string is not in the correct format, we return it without formatting
}
}
Use:
tfDate.text = strDate.formattedDate(inputFormat: "yyyy-MM-dd", outputFormat: "dd/MM/yyyy")