Advertisement
Guest User

Notification Date Format

a guest
Jul 27th, 2017
79
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Swift 1.32 KB | None | 0 0
  1. func notifDateOffset(from date: Date) -> String {
  2.     // Greater than 7 days:
  3.     // format: 19 August 2017 (dd MMMM yyyy)
  4.     if self.weeks(from: date) > 0 {
  5.         let toFormatter = DateFormatter()
  6.         toFormatter.dateFormat = "dd MMMM yyyy"
  7.         return toFormatter.string(from: date)
  8.     }
  9.    
  10.     // Yesterday and x days ago:
  11.     // possible formats:
  12.     //    - Yesterday
  13.     //    - 6 days ago
  14.     if self.days(from: date) > 0 {
  15.         let days = self.days(from: date)
  16.         if days == 1 {
  17.             return "Yesterday"
  18.         }
  19.         return "\(days) day\(days > 1 ? "s" : "") ago"
  20.     }
  21.    
  22.     // Today - Greater than an hour:
  23.     // format: 4 hours ago
  24.     if self.hours(from: date)   > 0 {
  25.         let hours = self.hours(from: date)
  26.         return "\(hours) hour\(hours > 1 ? "s" : "") ago"
  27.     }
  28.    
  29.     // Today - Less than an hour:
  30.     // possible formats:
  31.     //    - just now
  32.     //    - moments ago
  33.     //    - a minute ago
  34.     //    - 30 minutes ago
  35.     if self.minutes(from: date) > 0 {
  36.         let minutes = self.minutes(from: date)
  37.         if minutes == 1 {
  38.             return "a minute ago"
  39.         }
  40.         if minutes < 10 {
  41.             return "moments ago"
  42.         }
  43.         return "\(minutes) minute\(minutes > 1 ? "s" : "") ago"
  44.     }
  45.     return "just now"
  46. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement