Skip to content

Instantly share code, notes, and snippets.

@ciryon
Created August 24, 2015 11:38
Show Gist options
  • Save ciryon/a50bd7ebab7a3b131963 to your computer and use it in GitHub Desktop.
Save ciryon/a50bd7ebab7a3b131963 to your computer and use it in GitHub Desktop.
Time a go in words for swift
extension NSDate {
func timeAgoInWords() -> String {
let secondsDiff = self.timeIntervalSinceNow
if secondsDiff < 60 {
return "JUST_NOW".localized
}
if secondsDiff < 3600 {
let minutesAgo = secondsDiff / 60
return "X_MINS_AGO".localizedStringWithVariables(minutesAgo)
}
if secondsDiff < 3600*24 {
let hoursAgo = floor(secondsDiff / 3600)
return "X_HOURS_AGO".localizedStringWithVariables(hoursAgo)
}
else {
let hoursAgo = floor(secondsDiff / (3600*24))
return "X_DAYS_AGO".localizedStringWithVariables(hoursAgo)
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment