Created
May 25, 2016 02:33
-
-
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...
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| 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