4

I want to convert the date like "Friday 17th, 7:00pm" this format. but I am unable to add "th" and "st" after the date. I am using below code to convert the date format like this. please tell me. What I do for this format?

 func convertDateFormater(_ 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?
        //dateFormatter.locale = Locale(identifier: "your_loc_id")
        let convertedDate = dateFormatter.date(from: date)
        guard dateFormatter.date(from: date) != nil else {
            assert(false, "no date from string")
            return ""
        }
        dateFormatter.dateFormat = "EEEE dd, h:mma"///this is what you want to convert format
        dateFormatter.timeZone = NSTimeZone(name: "UTC") as TimeZone?
        let timeStamp = dateFormatter.string(from: convertedDate!)

        return timeStamp
    }
gaurav gupta
  • 115
  • 2
  • 11
  • see https://stackoverflow.com/questions/1283045/ordinal-month-day-suffix-option-for-nsdateformatter-setdateformat/31033712 – Sulthan Jun 02 '18 at 09:45
  • 4
    Possible duplicate of [Ordinal Month-day Suffix Option for NSDateFormatter setDateFormat](https://stackoverflow.com/questions/1283045/ordinal-month-day-suffix-option-for-nsdateformatter-setdateformat) – Anton Belousov Jun 02 '18 at 11:37

2 Answers2

5

As long as different locales do not play any role you could do something like this:

let date = Date()

let calendar = Calendar.current

let day = calendar.component(.day, from: date)
let daySuffix: String

switch day {
case 11...13: return "th"
default:
    switch day % 10 {
    case 1: return "st"
    case 2: return "nd"
    case 3: return "rd"
    default: return "th"
    }
}

let dateFormatter = DateFormatter()
dateFormatter.dateFormat = "EEEE dd'\(daySuffix)', h:mma"

dateFormatter.string(from: date)
André Slotta
  • 13,774
  • 2
  • 22
  • 34
0

Also, if this is being used in a static helper function, make sure to set the daySuffix instead of returning. My use case was very similar though I was using an epoch time pulled from our API, but I needed this function to be an Extension on the String class is an iOS Swift project. I ended up with the following:

    static func formatPrettyDate(_ startTime: Int) -> String {
        let date = Date(timeIntervalSince1970: TimeInterval(startTime / 1000))
        let calendar = Calendar.current
        let day = calendar.component(.day, from: date)
        let daySuffix: String

        switch day {
        case 11...13: daySuffix = "th"
        default:
            switch day % 10 {
            case 1: daySuffix = "st"
            case 2: daySuffix = "nd"
            case 3: daySuffix = "rd"
            default: daySuffix = "th"
            }
        }


        let dateFormatter = DateFormatter()
        dateFormatter.dateFormat = "EEEE, MMMM dd'\(daySuffix)'"
        return dateFormatter.string(from: date)
    }

This returned something like this: Thursday, August 27th and I am now able to use this on any UI string component that needs to have a pretty date from an epoch.

@AndréSlotta gave a great answer, so all credit should go to him. I just changed for my use case.

MiggityMac
  • 919
  • 9
  • 11