-4

I have a string "2020-01-01T00:00:00". How shall I convert it to another string of format "01-Jan-2020" in swift (iOS)?

The conversion is from String to String.

ABM
  • 565
  • 7
  • 18
  • 1
    Use a `DateFormatter` to convert the `String` to a `Date` and the use another `DateFormatter` to convert that to the `String` format you want - so you actually have two questions, both of which have been answered many times before - I suggest trying something first then, if you still have problems, post a question with the work you've tried – MadProgrammer Jan 24 '20 at 10:00

2 Answers2

2
let inputDate = "2020-01-01T00:00:00"

let dateFmt = DateFormatter()
dateFmt.dateFormat = "yyyy-MM-dd'T'HH:mm:ss"

if let date = dateFmt.date(from: inputDate) {
    dateFmt.dateFormat = "dd-MMM-yyyy"
    print(dateFmt.string(from: date))
}
Jaydeep Vora
  • 6,085
  • 1
  • 22
  • 40
1

try below function

func convertDateFormater(_ date: String, current_formate : String, expected_formate: String) -> String{
    let dateFormatter = DateFormatter()
    dateFormatter.dateFormat = current_formate
    let date = dateFormatter.date(from: date)
    dateFormatter.dateFormat = expected_formate
    return  dateFormatter.string(from: date!)
}
Chetan Hedamba
  • 293
  • 2
  • 9