Advertisement
Guest User

Untitled

a guest
Nov 12th, 2018
189
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Swift 8.48 KB | None | 0 0
  1. //
  2. //  Date+.swift
  3. //  UMobile
  4. //
  5. //  Created by John Eris Villanueva on 30/08/2017.
  6. //  Copyright © 2017 Donn Carlo Gamboa. All rights reserved.
  7. //
  8. import Foundation
  9. extension Date {
  10.     
  11.     public var apiFormat: String {
  12.         let dateFormatter = DateFormatter()
  13.         dateFormatter.dateFormat = kDateAPIRequestFormat
  14.         return dateFormatter.string(from: self)
  15.     }
  16.     
  17.     public var timeFormat: String {
  18.         let dateFormatter = DateFormatter()
  19.         dateFormatter.dateFormat = "hh:mma"
  20.         return dateFormatter.string(from: self)
  21.     }
  22.     
  23.     public var time24HourFormat: String {
  24.         let dateFormatter = DateFormatter()
  25.         dateFormatter.dateFormat = "HH:mm"
  26.         return dateFormatter.string(from: self)
  27.     }
  28.     
  29.     public var successDisplayFormat: String {
  30.         let dateFormatter = DateFormatter()
  31.         dateFormatter.dateFormat = kDateAPISuccessDisplayFormat
  32.         return dateFormatter.string(from: self)
  33.     }
  34.     
  35.     public var successNoSecondsDisplayFormat: String {
  36.         let dateFormatter = DateFormatter()
  37.         dateFormatter.dateFormat = kDateAPISuccessNoSecondsDisplayFormat
  38.         return dateFormatter.string(from: self)
  39.     }
  40.     
  41.     public var successDisplayDateOnlyFormat: String {
  42.         let dateFormatter = DateFormatter()
  43.         dateFormatter.dateFormat = kDateOnlyAPISuccessDisplayFormat
  44.         return dateFormatter.string(from: self)
  45.     }
  46.     
  47.     public var successDisplayTimeOnlyFormat: String {
  48.         let dateFormatter = DateFormatter()
  49.         dateFormatter.dateFormat = kTimeOnlyAPISuccessDisplayFormat
  50.         return dateFormatter.string(from: self)
  51.     }
  52.     
  53.     public var successDisplayFullDateFormat: String {
  54.         let dateFormatter = DateFormatter()
  55.         dateFormatter.dateFormat = kDateAPIResponseFormat
  56.         return dateFormatter.string(from: self)
  57.     }
  58.     
  59.     public var lastLoginDisplayFormat: String {
  60.         let dateFormatter = DateFormatter()
  61.         dateFormatter.dateFormat = kDateLastLoginDisplayFormat
  62.         return dateFormatter.string(from: self)
  63.     }
  64.     
  65.     public var monthDayYearDisplayFormat: String {
  66.         let dateFormatter = DateFormatter()
  67.         dateFormatter.dateFormat = kMonthDayYearDisplayFormat
  68.         return dateFormatter.string(from: self)
  69.     }
  70.     
  71.     public var monthYearDisplayFormat: String {
  72.         let dateFormatter = DateFormatter()
  73.         dateFormatter.dateFormat = kMonthYearDisplayFormat
  74.         return dateFormatter.string(from: self)
  75.     }
  76.     
  77.     /// Returns the amount of years from another date
  78.     public func years(from date: Date) -> Int {
  79.         return Calendar.current.dateComponents([.year], from: date, to: self).year ?? 0
  80.     }
  81.     
  82.     /// Returns the amount of months from another date
  83.     public func months(from date: Date) -> Int {
  84.         return Calendar.current.dateComponents([.month], from: date, to: self).month ?? 0
  85.     }
  86.     
  87.     /// Returns the amount of weeks from another date
  88.     public func weeks(from date: Date) -> Int {
  89.         return Calendar.current.dateComponents([.weekOfYear], from: date, to: self).weekOfYear ?? 0
  90.     }
  91.     
  92.     /// Returns the amount of days from another date
  93.     public func days(from date: Date) -> Int {
  94.         return Calendar.current.dateComponents([.day], from: date, to: self).day ?? 0
  95.     }
  96.     
  97.     /// Returns the amount of hours from another date
  98.     public func hours(from date: Date) -> Int {
  99.         return Calendar.current.dateComponents([.hour], from: date, to: self).hour ?? 0
  100.     }
  101.     
  102.     /// Returns the amount of minutes from another date
  103.     public func minutes(from date: Date) -> Int {
  104.         return Calendar.current.dateComponents([.minute], from: date, to: self).minute ?? 0
  105.     }
  106.     
  107.     /// Returns the amount of seconds from another date
  108.     public func seconds(from date: Date) -> Int {
  109.         return Calendar.current.dateComponents([.second], from: date, to: self).second ?? 0
  110.     }
  111.     
  112.     public func format(format: String) -> String {
  113.         let dateFormatter = DateFormatter()
  114.         dateFormatter.dateFormat = format
  115.         return dateFormatter.string(from: self)
  116.     }
  117.     
  118.     public func daySuffix() -> String {
  119.         let calendar = Calendar.current
  120.         let dayOfMonth = calendar.component(.day, from: self)
  121.         switch dayOfMonth {
  122.         case 1, 21, 31: return "st"
  123.         case 2, 22: return "nd"
  124.         case 3, 23: return "rd"
  125.         default: return "th"
  126.         }
  127.     }
  128.     
  129.     public func dateOffset(from date: Date) -> String {
  130.         if self.years(from: date) > 0 {
  131.             let years = self.years(from: date)
  132.             return "\(years) year\(years > 1 ? "s" : "") ago"
  133.         }
  134.         if self.months(from: date)  > 0 {
  135.             let months = self.months(from: date)
  136.             return "\(months) month\(months > 1 ? "s" : "") ago"
  137.         }
  138.         if self.weeks(from: date)   > 0 {
  139.             let weeks = self.weeks(from: date)
  140.             return "\(weeks) week\(weeks > 1 ? "s" : "") ago"
  141.         }
  142.         if self.days(from: date)    > 0 {
  143.             let days = self.days(from: date)
  144.             if days == 1 {
  145.                 return "Yesterday"
  146.             }
  147.             return "\(days) day\(days > 1 ? "s" : "") ago"
  148.         }
  149.         if self.hours(from: date)   > 0 {
  150.             let hours = self.hours(from: date)
  151.             return "\(hours) hour\(hours > 1 ? "s" : "") ago"
  152.         }
  153.         if self.minutes(from: date) > 0 {
  154.             let minutes = self.minutes(from: date)
  155.             if minutes == 1 {
  156.                 return "a minute ago"
  157.             }
  158.             if minutes < 10 {
  159.                 return "moments ago"
  160.             }
  161.             return "\(minutes) minute\(minutes > 1 ? "s" : "") ago"
  162.         }
  163.         return "just now"
  164.     }
  165.     
  166.     public func notifDateOffset(from date: Date) -> String {
  167.         // Greater than 7 days:
  168.         // format: 19 August 2017 (dd MMMM yyyy)
  169.         if self.weeks(from: date) > 0 {
  170.             let toFormatter = DateFormatter()
  171.             toFormatter.dateFormat = "dd MMMM yyyy"
  172.             return toFormatter.string(from: date)
  173.         }
  174.         
  175.         // Yesterday and x days ago:
  176.         // possible formats:
  177.         //    - Yesterday
  178.         //    - 6 days ago
  179.         if self.days(from: date) > 0 {
  180.             let days = self.days(from: date)
  181.             if days == 1 {
  182.                 return "Yesterday"
  183.             }
  184.             return "\(days) day\(days > 1 ? "s" : "") ago"
  185.         }
  186.         
  187.         // Today - Greater than an hour:
  188.         // format: 4 hours ago
  189.         if self.hours(from: date)   > 0 {
  190.             let hours = self.hours(from: date)
  191.             return "\(hours) hour\(hours > 1 ? "s" : "") ago"
  192.         }
  193.         
  194.         // Today - Less than an hour:
  195.         // possible formats:
  196.         //    - just now
  197.         //    - moments ago
  198.         //    - a minute ago
  199.         //    - 30 minutes ago
  200.         if self.minutes(from: date) > 0 {
  201.             let minutes = self.minutes(from: date)
  202.             if minutes == 1 {
  203.                 return "A minute ago"
  204.             }
  205.             if minutes < 10 {
  206.                 return "Moments ago"
  207.             }
  208.             return "\(minutes) minute\(minutes > 1 ? "s" : "") ago"
  209.         }
  210.         return "Just now"
  211.     }
  212.     
  213.     public var dayOfTheWeek: String {
  214.         let dateFormatter = DateFormatter()
  215.         dateFormatter.dateFormat = "EEEE"
  216.         return dateFormatter.string(from: self)
  217.     }
  218.     
  219.     public var dateOfTheMonth: String {
  220.         let dateFormatter = DateFormatter()
  221.         dateFormatter.dateFormat = "dd"
  222.         return dateFormatter.string(from: self)
  223.     }
  224. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement