Ordinal DateFormate Like - "11th" , "21st" in swift 3.0.

An Ordinal Number is the position of the number list such as 1st, 2nd , 3rd , 4th, 5th, 6th etc. Means ordinal number ends with "st", "nd" , "rd" , and "th".

An Ordinal Number is a number that tells the position of something in a list, such as 1st, 2nd, 3rd, 4th, 5th etc. Most ordinal numbers end in "th" except for: one ⇒ first (1st) two ⇒ second (2nd) three ⇒ third (3rd).


The function of ordinary date format in swift 3.0
func convertDateFormate(date : Date) -> String{
    // Day
    let calendar = Calendar.current
    let anchorComponents = calendar.dateComponents([.day, .month, .year], from: date)

    // Formate
    let dateFormate = DateFormatter()
    dateFormate.dateFormat = "MMM, yyyy"
    let newDate = dateFormate.string(from: date)

    var day  = "\(anchorComponents.day!)"
    switch (day) {
    case "1" , "21" , "31":
        day.append("st")
    case "2" , "22":
        day.append("nd")
    case "3" ,"23":
        day.append("rd")
    default:
        day.append("th")
    }
    return day + " " + newDate
}
Use the above ordinary function.
let myCurrentDate = self.convertDateFormate(date: Date())
print(myCurrentDate)

Output of ordinary function.


11th Dec, 2016
30th Dec, 2016
31st Dec, 2016

Thanks.

Ordinal DateFormate Like - "11th" , "21st" in swift 3.0. Ordinal DateFormate Like - "11th" , "21st" in swift 3.0. Reviewed by Kirit Modi on 21:54:00 Rating: 5

No comments:

Powered by Blogger.