Date based get the strings as like 21st or 3rd or 5th iOS swift

 Problem:

I want function that if i pass the date to the function that will return like 21st or 3rd or 5th .

Solution:

extension Date {

    func daySuffix() -> String {

        let calendar = Calendar.current

        let components = (calendar as NSCalendar).components(.day, from: self)

        let dayOfMonth = components.day

        switch dayOfMonth {

        case 1, 21, 31:

            return "st"

        case 2, 22:

            return "nd"

        case 3, 23:

            return "rd"

        default:

            return "th"

        }

    }

}


Call the above function as like below.


mydate.daySuffix()

Comments

Popular posts from this blog

Invalid bundle error while upload the app to the app Store

Convert NsNumber, NSDate to String in iOS Swift

Global LocationManager Singleton class iOS Swift