Skip to content

Instantly share code, notes, and snippets.

@williamhqs
Created December 6, 2018 03:38
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 williamhqs/81469b887f3eadcf6e1557da648f2508 to your computer and use it in GitHub Desktop.
Save williamhqs/81469b887f3eadcf6e1557da648f2508 to your computer and use it in GitHub Desktop.
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)))
@williamhqs
Copy link
Author

williamhqs commented Dec 6, 2018

extension Date {
    var timestampString: String? {
        let formatter = DateComponentsFormatter()
        formatter.unitsStyle = .abbreviated
        formatter.maximumUnitCount = 1
        formatter.allowedUnits = [.year, .month, .day, .hour, .minute, .second]
        return timeString?.formatter.string(from: self, to: Date()
    }
}

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment