Created
April 18, 2016 22:00
-
-
Save soffes/bc217f2057549c01efe0a937e0dcdc0d to your computer and use it in GitHub Desktop.
Naïve time ago in words
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
| import Foundation | |
| extension NSDate { | |
| var briefTimeAgoInWords: String { | |
| let components = NSCalendar.currentCalendar().components([.Second, .Minute, .Hour, .Day, .Year], fromDate: self, toDate: NSDate(), options: []) | |
| if components.year > 0 { | |
| return "\(components.year)y" | |
| } | |
| if components.day > 0 { | |
| return "\(components.day)d" | |
| } | |
| if components.hour > 0 { | |
| return "\(components.hour)h" | |
| } | |
| if components.minute > 0 { | |
| return "\(components.minute)m" | |
| } | |
| return "\(components.second)s" | |
| } | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment