How to add th and st in Date Format

独自空忆成欢 提交于 2021-01-27 07:35:50

问题


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
    }

回答1:


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)



回答2:


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.



来源:https://stackoverflow.com/questions/50655374/how-to-add-th-and-st-in-date-format

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!