how i can implement this date format 1st July,2021 17:00.My current code is
func changeFormat(_ toFormat: String,_ dateStr: Date?) -> String{
let date = dateStr ?? Date()
let dateFormatter = DateFormatter()
dateFormatter.dateFormat = toFormat
return dateFormatter.string(from: date)
}
func covertStringToDate(_ date: String) -> Date {
let isoDate = date
let dateFormatter = DateFormatter()
dateFormatter.locale = Locale(identifier: "en_US_POSIX") // set locale to reliable US_POSIX
dateFormatter.dateFormat = "yyyy-MM-dd"
return dateFormatter.date(from:isoDate)!
}
2
Answers
If u want to convert any
Date
object to this format do this.If u want to convert this type of date (
1st July,2021 17:00
) to aDate
object do this.If you want to use localisation support, try this template(playground):