Skip to content

Instantly share code, notes, and snippets.

@kgn
Created May 25, 2016 02:33
Show Gist options
  • Select an option

  • Save kgn/4525b06db2cc09d7caa9c50cb42761ee to your computer and use it in GitHub Desktop.

Select an option

Save kgn/4525b06db2cc09d7caa9c50cb42761ee to your computer and use it in GitHub Desktop.
Add 'st', 'nd', 'rd' and 'th' to data strings: May 17th, April 2rd...
let dateFormatter = NSDateFormatter()
dateFormatter.dateFormat = "MMMM d"
private func daySuffix(date: NSDate) -> String {
let calendar = NSCalendar.currentCalendar()
let dayOfMonth = calendar.component(.Day, fromDate: date)
switch dayOfMonth {
case 1: fallthrough
case 21: fallthrough
case 31: return "st"
case 2: fallthrough
case 22: return "nd"
case 3: fallthrough
case 23: return "rd"
default: return "th"
}
}
let dateComp = NSDateComponents()
dateComp.day = -10
let date = NSCalendar.currentCalendar().dateByAddingComponents(dateComp, toDate: NSDate(), options: [])!
dateFormatter.stringFromDate(date)+daySuffix(date)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment