Created
December 6, 2018 03:38
-
-
Save williamhqs/81469b887f3eadcf6e1557da648f2508 to your computer and use it in GitHub Desktop.
This file contains hidden or 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 Date { | |
func yearsCount(from date: Date) -> Int { | |
return Calendar.current.dateComponents([.year], from: date, to: self).year ?? 0 | |
} | |
func monthsCount(from date: Date) -> Int { | |
return Calendar.current.dateComponents([.month], from: date, to: self).month ?? 0 | |
} | |
func weeksCount(from date: Date) -> Int { | |
return Calendar.current.dateComponents([.weekOfMonth], from: date, to: self).weekOfMonth ?? 0 | |
} | |
func daysCount(from date: Date) -> Int { | |
return Calendar.current.dateComponents([.day], from: date, to: self).day ?? 0 | |
} | |
func hoursCount(from date: Date) -> Int { | |
return Calendar.current.dateComponents([.hour], from: date, to: self).hour ?? 0 | |
} | |
func minutesCount(from date: Date) -> Int { | |
return Calendar.current.dateComponents([.minute], from: date, to: self).minute ?? 0 | |
} | |
func secondsCount(from date: Date) -> Int { | |
return Calendar.current.dateComponents([.second], from: date, to: self).second ?? 0 | |
} | |
func timeAgo1(from date: Date) -> String { | |
if yearsCount(from: date) > 0 { return "\(yearsCount(from: date)) \(yearsCount(from: date) == 1 ? "year" : "years") ago" } | |
if monthsCount(from: date) > 0 { return "\(monthsCount(from: date)) \(monthsCount(from: date) == 1 ? "month" : "months") ago" } | |
if weeksCount(from: date) > 0 { return "\(weeksCount(from: date)) \(weeksCount(from: date) == 1 ? "week" : "weeks") ago" } | |
if daysCount(from: date) > 0 { return "\(daysCount(from: date)) \(daysCount(from: date) == 1 ? "day" : "days") ago" } | |
if hoursCount(from: date) > 0 { return "\(hoursCount(from: date)) \(hoursCount(from: date) == 1 ? "hour" : "hours") ago" } | |
if minutesCount(from: date) > 0 { return "\(minutesCount(from: date)) \(minutesCount(from: date) == 1 ? "minute" : "minutes") ago" } | |
if secondsCount(from: date) > 0 { return "\(secondsCount(from: date)) \(secondsCount(from: date) == 1 ? "second" : "seconds") ago" } | |
return "" | |
} | |
func timeAgo(from date: Date) -> String { | |
if yearsCount(from: date) > 0 { return "\(yearsCount(from: date))y ago" } | |
if monthsCount(from: date) > 0 { return "\(monthsCount(from: date))M ago" } | |
if weeksCount(from: date) > 0 { return "\(weeksCount(from: date))w ago" } | |
if daysCount(from: date) > 0 { return "\(daysCount(from: date))d ago" } | |
if hoursCount(from: date) > 0 { return "\(hoursCount(from: date))h ago" } | |
if minutesCount(from: date) > 0 { return "\(minutesCount(from: date))m ago" } | |
if secondsCount(from: date) > 0 { return "\(secondsCount(from: date))s ago" } | |
return "" | |
} | |
} | |
let hour = 60 * 60 | |
let day = 24 * hour | |
let week = 7 * day | |
let month = 9 * week | |
let year = 730 * day | |
let date = Date() | |
Date().timeAgo(from: Date().addingTimeInterval(-TimeInterval(year))) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Uh oh!
There was an error while loading. Please reload this page.