DateFormatter doesn’t support the ordinal date format 1st, 2nd you have to do it yourself for example

static func formatDateFromDateTimeString(dateString: String, inputFormat: String = "yyyy-MM-dd'T'HH:mm:ss") -> String? {
    let dateFormatter = DateFormatter()
    dateFormatter.locale = Locale(identifier: "en_US_POSIX")
    dateFormatter.dateFormat = inputFormat
    guard let date = dateString) else { return nil }
    let day = Calendar.current.component(.day, from: date)
    dateFormatter.dateFormat = "MMM, yyyy"
    let numberFormatter = NumberFormatter()
    numberFormatter.numberStyle = .ordinal
    guard let dayString = numberFormatter.string(from: day as NSNumber) else { return nil }
    return dayString + " " + dateFormatter.string(from: date)

formatDateFromDateTimeString(dateString: "2 January 2020", inputFormat: "dd MMMM yyyy") 

The outPutFormat parameter makes no sense if you want this particular date format.

CLICK HERE to find out more related problems solutions.

Leave a Comment

Your email address will not be published.

Scroll to Top