Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- func notifDateOffset(from date: Date) -> String {
- // Greater than 7 days:
- // format: 19 August 2017 (dd MMMM yyyy)
- if self.weeks(from: date) > 0 {
- let toFormatter = DateFormatter()
- toFormatter.dateFormat = "dd MMMM yyyy"
- return toFormatter.string(from: date)
- }
- // Yesterday and x days ago:
- // possible formats:
- // - Yesterday
- // - 6 days ago
- if self.days(from: date) > 0 {
- let days = self.days(from: date)
- if days == 1 {
- return "Yesterday"
- }
- return "\(days) day\(days > 1 ? "s" : "") ago"
- }
- // Today - Greater than an hour:
- // format: 4 hours ago
- if self.hours(from: date) > 0 {
- let hours = self.hours(from: date)
- return "\(hours) hour\(hours > 1 ? "s" : "") ago"
- }
- // Today - Less than an hour:
- // possible formats:
- // - just now
- // - moments ago
- // - a minute ago
- // - 30 minutes ago
- if self.minutes(from: date) > 0 {
- let minutes = self.minutes(from: date)
- if minutes == 1 {
- return "a minute ago"
- }
- if minutes < 10 {
- return "moments ago"
- }
- return "\(minutes) minute\(minutes > 1 ? "s" : "") ago"
- }
- return "just now"
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement