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

store cgpoint in userdefaults iOS swift

libc++abi: terminating due to uncaught exception of type NSException iOS swift