Skip to content

Instantly share code, notes, and snippets.

@julesjans
Created August 17, 2017 10:29
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save julesjans/722441c662dd1f1985cd97aba324d279 to your computer and use it in GitHub Desktop.
Save julesjans/722441c662dd1f1985cd97aba324d279 to your computer and use it in GitHub Desktop.
Human "time ago" for swift
import Foundation
extension Date {
func timeAgo() -> String {
let calendar = Calendar.current
let seconds = calendar.dateComponents([.second], from: self, to: Date()).second!
let minutes = calendar.dateComponents([.minute], from: self, to: Date()).minute!
let hours = calendar.dateComponents([.hour], from: self, to: Date()).hour!
let days = calendar.dateComponents([.day], from: self, to: Date()).day!
let weeks = calendar.dateComponents([.weekOfMonth], from: self, to: Date()).weekOfMonth!
let months = calendar.dateComponents([.month], from: self, to: Date()).month!
let years = calendar.dateComponents([.year], from: self, to: Date()).year!
if (seconds < 60) {
return NSLocalizedString("Just now", comment: "Very recent")
} else if (minutes < 60) {
return "\(minutes)\(NSLocalizedString("m ago", comment: "Minutes ago"))"
} else if (hours < 24) {
return (hours == 1) ? "\(hours)\(NSLocalizedString("hr ago", comment: "One hour ago"))" : "\(hours)\(NSLocalizedString("hrs ago", comment: "Hours ago"))"
} else if (days < 14) {
return (days == 1) ? "\(days) \(NSLocalizedString("day ago", comment: "One day ago"))" : "\(days) \(NSLocalizedString("days ago", comment: "Days ago"))"
} else if (days < 31) {
return (weeks == 1) ? "\(weeks) \(NSLocalizedString("week ago", comment: "One week ago"))" : "\(weeks) \(NSLocalizedString("weeks ago", comment: "Weeks ago"))"
} else if (months < 12) {
return (months == 1) ? "\(months) \(NSLocalizedString("month ago", comment: "One month ago"))" : "\(months) \(NSLocalizedString("months ago", comment: "Months ago"))"
} else {
return (years == 1) ? "\(years) \(NSLocalizedString("year ago", comment: "One year ago"))" : "\(years) \(NSLocalizedString("years ago", comment: "Years ago"))"
}
}
}
extension NSDate {
func timeAgo() -> String {
let date = self as Date
return date.timeAgo()
}
}
var date = Date()
let nsdate = NSDate()
nsdate.timeAgo() == "Just now"
DateFormatter.localizedString(from: nsdate as Date, dateStyle: .short, timeStyle: .short)
date = Date(timeInterval: -1, since: Date())
date.timeAgo() == "Just now"
date = Date(timeInterval: -60, since: Date())
date.timeAgo() == "1m ago"
print(date.timeAgo())
date.timeAgo() == "3m ago"
date = Date(timeInterval: -(60 * 60), since: Date())
date.timeAgo() == "1hr ago"
date = Date(timeInterval: -((60 * 60) * 3), since: Date())
date.timeAgo() == "3hrs ago"
date = Date(timeInterval: -(60 * 60 * 24), since: Date())
date.timeAgo() == "1 day ago"
date = Date(timeInterval: -(((60 * 60 * 24) * 7)), since: Date())
date.timeAgo() == "7 days ago"
date = Date(timeInterval: -((60 * 60 * 24) * 13), since: Date())
date.timeAgo() == "13 days ago"
date = Date(timeInterval: -((60 * 60 * 24) * 14), since: Date())
date.timeAgo() == "2 weeks ago"
date = Date(timeInterval: -(((60 * 60 * 24) * 22)), since: Date())
date.timeAgo() == "3 weeks ago"
date = Date(timeInterval: -(((60 * 60 * 24) * 31)), since: Date())
date.timeAgo() == "1 month ago"
date = Date(timeInterval: -((((60 * 60 * 24) * 31) * 4)), since: Date())
date.timeAgo() == "4 months ago"
date = Date(timeInterval: -((60 * 60 * 24) * 366), since: Date())
date.timeAgo() == "1 year ago"
date = Date(timeInterval: -(((60 * 60 * 24) * 366) * 5), since: Date())
date.timeAgo() == "5 years ago"
date = Date(timeInterval: -((60 * 60 * 24) * 365), since: Date())
print(date.timeAgo())
date = Date(timeInterval: -(((60 * 60 * 24) * 365) * 5), since: Date())
print(date.timeAgo())
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment