Skip to content

Instantly share code, notes, and snippets.

@hanishassim
Last active November 1, 2018 11:20
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save hanishassim/f57c01fbf7aadc406d683267aec488c8 to your computer and use it in GitHub Desktop.
Save hanishassim/f57c01fbf7aadc406d683267aec488c8 to your computer and use it in GitHub Desktop.
Time Ago Swift Date Extension
//Credit to https://gist.github.com/merlos All copyright & credit goes to him
import Foundation
extension Date {
///
/// Provides a humanised date. For instance: 1 minute, 1 week ago, 3 months ago
///
/// - Parameters:
// - numericDates: Set it to true to get "1 year ago", "1 month ago" or false if you prefer "Last year", "Last month"
///
func timeAgo(numericDates:Bool) -> String {
let calendar = Calendar.current
let now = Date()
let earliest = self < now ? self : now
let latest = self > now ? self : now
let unitFlags: Set<Calendar.Component> = [.minute, .hour, .day, .weekOfMonth, .month, .year, .second]
let components: DateComponents = calendar.dateComponents(unitFlags, from: earliest, to: latest)
//print("")
//print(components)
if let year = components.year {
if (year >= 2) {
return "\(year) years ago"
} else if (year >= 1) {
return numericDates ? "1 year ago" : "Last year"
}
}
if let month = components.month {
if (month >= 2) {
return "\(month) months ago"
} else if (month >= 1) {
return numericDates ? "1 month ago" : "Last month"
}
}
if let weekOfMonth = components.weekOfMonth {
if (weekOfMonth >= 2) {
return "\(weekOfMonth) weeks ago"
} else if (weekOfMonth >= 1) {
return numericDates ? "1 week ago" : "Last week"
}
}
if let day = components.day {
if (day >= 2) {
return "\(day) days ago"
} else if (day >= 1) {
return numericDates ? "1 day ago" : "Yesterday"
}
}
if let hour = components.hour {
if (hour >= 2) {
return "\(hour) hours ago"
} else if (hour >= 1) {
return numericDates ? "1 hour ago" : "An hour ago"
}
}
if let minute = components.minute {
if (minute >= 2) {
return "\(minute) minutes ago"
} else if (minute >= 1) {
return numericDates ? "1 minute ago" : "A minute ago"
}
}
if let second = components.second {
if (second >= 3) {
return "\(second) seconds ago"
}
}
return "Just now"
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment