Created
August 24, 2015 11:38
-
-
Save ciryon/a50bd7ebab7a3b131963 to your computer and use it in GitHub Desktop.
Time a go in words for swift
This file contains 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
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