Created
August 17, 2017 10:29
-
-
Save julesjans/722441c662dd1f1985cd97aba324d279 to your computer and use it in GitHub Desktop.
Human "time ago" for swift
This file contains 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
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