I am trying to convert string to date and then back to string but I always get a fatal error.
let dateFormatter = DateFormatter()
dateFormatter.dateFormat = "EEE, dd MMM yyyy hh:mm:ss Z" //Your date format
dateFormatter.locale = Locale(identifier: "en_US")
//dateFormatter.timeZone = TimeZone(abbreviation: "GMT+0:00") //Current time zone
//according to date format your date string
guard let date = dateFormatter.date(from: "Tue, 21 Jul 2020 20:04:09 +0000") else {
fatalError()
}
print(date) //Convert String to Date
dateFormatter.dateFormat = "MMM d, yyyy" //Your New Date format as per requirement change it own
let newDate = dateFormatter.string(from: date) //pass Date here
print(newDate) //New formatted Date string
return newDate
I know there are so many solutions, but none of them are working.