Skip to content

Instantly share code, notes, and snippets.

@levantAJ
Last active May 5, 2017 11:06
Show Gist options
  • Save levantAJ/8fea1be4fa3c32cd82066b0f58b165d7 to your computer and use it in GitHub Desktop.
Save levantAJ/8fea1be4fa3c32cd82066b0f58b165d7 to your computer and use it in GitHub Desktop.
extension Date {
var timeAgoSimple: String {
let deltaSeconds = fabs(timeIntervalSince(Date()))
let deltaMinutes = deltaSeconds / 60.0
if deltaSeconds < 60 {
return "\(Int(deltaSeconds))s"
} else if deltaMinutes < 60 {
return "\(Int(deltaMinutes))m"
} else if deltaMinutes < (24 * 60) {
return "\(Int(floor(deltaMinutes/60)))h"
} else if deltaMinutes < (24 * 60 * 7) {
return "\(Int(floor(deltaMinutes/(60 * 24))))d"
} else if deltaMinutes < (24 * 60 * 31) {
return "\(Int(floor(deltaMinutes/(60 * 24 * 7))))w"
} else if deltaMinutes < (24 * 60 * 365.25) {
return "\(Int(floor(deltaMinutes/(60 * 24 * 30))))mo"
}
return "\(Int(floor(deltaMinutes/(60 * 24 * 365))))yr"
}
var timeAgo: String {
let deltaSeconds = fabs(timeIntervalSince(Date()))
let deltaMinutes = deltaSeconds / 60.0
if deltaSeconds < 5 {
return "Just now"
} else if deltaSeconds < 60 {
return "\(Int(deltaSeconds)) seconds ago"
} else if deltaSeconds < 120 {
return "A minute ago"
} else if deltaMinutes < 60 {
return "\(Int(deltaMinutes)) minutes ago"
} else if deltaMinutes < 120 {
return "An hour ago"
} else if deltaMinutes < (24 * 60) {
return "\(Int(floor(deltaMinutes/60))) hours ago"
} else if deltaMinutes < (24 * 60 * 2) {
return "Yesterday"
} else if deltaMinutes < (24 * 60 * 7) {
return "\(Int(floor(deltaMinutes/(60 * 24)))) days ago"
} else if deltaMinutes < (24 * 60 * 14) {
return "Last week"
} else if deltaMinutes < (24 * 60 * 31) {
return "\(Int(floor(deltaMinutes/(60 * 24 * 7)))) weeks ago"
} else if deltaMinutes < (24 * 60 * 61) {
return "Last month"
} else if deltaMinutes < (24 * 60 * 365.25) {
return "\(Int(floor(deltaMinutes/(60 * 24 * 30)))) months ago"
} else if deltaMinutes < (24 * 60 * 731) {
return "Last year"
}
return "\(Int(floor(deltaMinutes/(60 * 24 * 365)))) years ago"
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment