Skip to content

Instantly share code, notes, and snippets.

@soffes
Created April 18, 2016 22:00
Show Gist options
  • Select an option

  • Save soffes/bc217f2057549c01efe0a937e0dcdc0d to your computer and use it in GitHub Desktop.

Select an option

Save soffes/bc217f2057549c01efe0a937e0dcdc0d to your computer and use it in GitHub Desktop.
Naïve time ago in words
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