I am using date picker in my application. After selects particular date, It’s not able to converting to required format.
Datepicker date:
2021-07-21 13:53:38 +0000
expected format
21 July 2021
I am trying with below code
static func formatStringToDate(date: String) -> String? {
let dateFormatter = DateFormatter()
dateFormatter.dateFormat = "yyyy-MM-dd'T'HH:mm:ss.SSSZ"//this your string date format
dateFormatter.timeZone = NSTimeZone(name: "UTC") as TimeZone?
let datee = dateFormatter.date(from: "2021-07-21 13:53:38 +0000")
dateFormatter.dateFormat = "d MMM yyyy"///this is what you want to convert format
dateFormatter.timeZone = NSTimeZone(name: "UTC") as TimeZone?
let timeStamp = dateFormatter.string(from: datee!)
return timeStamp ?? ""
}
Any suggestions?
2
Answers
I have found solution. Here it is
Actually the correct way to write this to get you desired format should be: