Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Ordinary Date Format "13th June 2007" in Swift [duplicate]

hello can any one point me to hoe make a date format that looks like this: 13th June 2007. Notice the day has the word "th". I want to add that in my format. Thanks!

like image 910
macky12345 Avatar asked May 31 '16 02:05

macky12345


1 Answers

Updated for Swift 3

You can do something like this which is locale safe.

let calendar = Calendar.current
let date = Date()
let dateComponents = calendar.component(.day, from: date)
let numberFormatter = NumberFormatter()

numberFormatter.numberStyle = .ordinal

let day = numberFormatter.string(from: dateComponents as NSNumber)
let dateFormatter = DateFormatter()

dateFormatter.dateFormat = "MMM yyyy"

let dateString = "\(day!) \(dateFormatter.string(from: date))"

print(dateString)
like image 114
Desdenova Avatar answered Oct 16 '22 17:10

Desdenova