Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- //
- // Date+.swift
- // UMobile
- //
- // Created by John Eris Villanueva on 30/08/2017.
- // Copyright © 2017 Donn Carlo Gamboa. All rights reserved.
- //
- import Foundation
- extension Date {
- public var apiFormat: String {
- let dateFormatter = DateFormatter()
- dateFormatter.dateFormat = kDateAPIRequestFormat
- return dateFormatter.string(from: self)
- }
- public var timeFormat: String {
- let dateFormatter = DateFormatter()
- dateFormatter.dateFormat = "hh:mma"
- return dateFormatter.string(from: self)
- }
- public var time24HourFormat: String {
- let dateFormatter = DateFormatter()
- dateFormatter.dateFormat = "HH:mm"
- return dateFormatter.string(from: self)
- }
- public var successDisplayFormat: String {
- let dateFormatter = DateFormatter()
- dateFormatter.dateFormat = kDateAPISuccessDisplayFormat
- return dateFormatter.string(from: self)
- }
- public var successNoSecondsDisplayFormat: String {
- let dateFormatter = DateFormatter()
- dateFormatter.dateFormat = kDateAPISuccessNoSecondsDisplayFormat
- return dateFormatter.string(from: self)
- }
- public var successDisplayDateOnlyFormat: String {
- let dateFormatter = DateFormatter()
- dateFormatter.dateFormat = kDateOnlyAPISuccessDisplayFormat
- return dateFormatter.string(from: self)
- }
- public var successDisplayTimeOnlyFormat: String {
- let dateFormatter = DateFormatter()
- dateFormatter.dateFormat = kTimeOnlyAPISuccessDisplayFormat
- return dateFormatter.string(from: self)
- }
- public var successDisplayFullDateFormat: String {
- let dateFormatter = DateFormatter()
- dateFormatter.dateFormat = kDateAPIResponseFormat
- return dateFormatter.string(from: self)
- }
- public var lastLoginDisplayFormat: String {
- let dateFormatter = DateFormatter()
- dateFormatter.dateFormat = kDateLastLoginDisplayFormat
- return dateFormatter.string(from: self)
- }
- public var monthDayYearDisplayFormat: String {
- let dateFormatter = DateFormatter()
- dateFormatter.dateFormat = kMonthDayYearDisplayFormat
- return dateFormatter.string(from: self)
- }
- public var monthYearDisplayFormat: String {
- let dateFormatter = DateFormatter()
- dateFormatter.dateFormat = kMonthYearDisplayFormat
- return dateFormatter.string(from: self)
- }
- /// Returns the amount of years from another date
- public func years(from date: Date) -> Int {
- return Calendar.current.dateComponents([.year], from: date, to: self).year ?? 0
- }
- /// Returns the amount of months from another date
- public func months(from date: Date) -> Int {
- return Calendar.current.dateComponents([.month], from: date, to: self).month ?? 0
- }
- /// Returns the amount of weeks from another date
- public func weeks(from date: Date) -> Int {
- return Calendar.current.dateComponents([.weekOfYear], from: date, to: self).weekOfYear ?? 0
- }
- /// Returns the amount of days from another date
- public func days(from date: Date) -> Int {
- return Calendar.current.dateComponents([.day], from: date, to: self).day ?? 0
- }
- /// Returns the amount of hours from another date
- public func hours(from date: Date) -> Int {
- return Calendar.current.dateComponents([.hour], from: date, to: self).hour ?? 0
- }
- /// Returns the amount of minutes from another date
- public func minutes(from date: Date) -> Int {
- return Calendar.current.dateComponents([.minute], from: date, to: self).minute ?? 0
- }
- /// Returns the amount of seconds from another date
- public func seconds(from date: Date) -> Int {
- return Calendar.current.dateComponents([.second], from: date, to: self).second ?? 0
- }
- public func format(format: String) -> String {
- let dateFormatter = DateFormatter()
- dateFormatter.dateFormat = format
- return dateFormatter.string(from: self)
- }
- public func daySuffix() -> String {
- let calendar = Calendar.current
- let dayOfMonth = calendar.component(.day, from: self)
- switch dayOfMonth {
- case 1, 21, 31: return "st"
- case 2, 22: return "nd"
- case 3, 23: return "rd"
- default: return "th"
- }
- }
- public func dateOffset(from date: Date) -> String {
- if self.years(from: date) > 0 {
- let years = self.years(from: date)
- return "\(years) year\(years > 1 ? "s" : "") ago"
- }
- if self.months(from: date) > 0 {
- let months = self.months(from: date)
- return "\(months) month\(months > 1 ? "s" : "") ago"
- }
- if self.weeks(from: date) > 0 {
- let weeks = self.weeks(from: date)
- return "\(weeks) week\(weeks > 1 ? "s" : "") 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"
- }
- if self.hours(from: date) > 0 {
- let hours = self.hours(from: date)
- return "\(hours) hour\(hours > 1 ? "s" : "") 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"
- }
- public 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"
- }
- public var dayOfTheWeek: String {
- let dateFormatter = DateFormatter()
- dateFormatter.dateFormat = "EEEE"
- return dateFormatter.string(from: self)
- }
- public var dateOfTheMonth: String {
- let dateFormatter = DateFormatter()
- dateFormatter.dateFormat = "dd"
- return dateFormatter.string(from: self)
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement