Date+Extension.swift 6.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223
  1. //
  2. // Date+Extension.swift
  3. // O2Platform
  4. //
  5. // Created by FancyLou on 2018/7/26.
  6. // Copyright © 2018 zoneland. All rights reserved.
  7. //
  8. import Foundation
  9. extension Date {
  10. func add(component: Calendar.Component, value: Int) -> Date {
  11. return Calendar.current.date(byAdding: component, value: value, to: self)!
  12. }
  13. var startOfDay: Date {
  14. return Calendar.current.startOfDay(for: self)
  15. }
  16. func getDateWithDay() -> Int {
  17. return NSCalendar.current.component(.day, from: self)
  18. }
  19. func getDateWithMonth() -> Int {
  20. return NSCalendar.current.component(.month, from: self)
  21. }
  22. func getDateWithYear() -> Int {
  23. return NSCalendar.current.component(.year, from: self)
  24. }
  25. /// 判断当前日期是否为今年
  26. // func isThisYear() -> Bool {
  27. // // 获取当前日历
  28. // let calender = Calendar.current
  29. // // 获取日期的年份
  30. // let yearComps = calender.component(.year, from: self)
  31. // // 获取现在的年份
  32. // let nowComps = calender.component(.year, from: Date())
  33. //
  34. // return yearComps == nowComps
  35. // }
  36. /// 是否是昨天
  37. // func isYesterday() -> Bool {
  38. // // 获取当前日历
  39. // let calender = Calendar.current
  40. // // 获取日期的年份
  41. // let comps = calender.dateComponents([.year, .month, .day], from: self, to: Date())
  42. // // 根据头条显示时间 ,我觉得可能有问题 如果comps.day == 0 显示相同,如果是 comps.day == 1 显示时间不同
  43. // // 但是 comps.day == 1 才是昨天 comps.day == 2 是前天
  44. // // return comps.year == 0 && comps.month == 0 && comps.day == 1
  45. // return comps.year == 0 && comps.month == 0 && comps.day == 0
  46. // }
  47. /// 是否是前天
  48. func isBeforeYesterday() -> Bool {
  49. // 获取当前日历
  50. let calender = Calendar.current
  51. // 获取日期的年份
  52. let comps = calender.dateComponents([.year, .month, .day], from: self, to: Date())
  53. //
  54. // return comps.year == 0 && comps.month == 0 && comps.day == 2
  55. return comps.year == 0 && comps.month == 0 && comps.day == 1
  56. }
  57. /// 判断是否是今天
  58. // func isToday() -> Bool {
  59. // // 日期格式化
  60. // let formatter = DateFormatter()
  61. // // 设置日期格式
  62. // formatter.dateFormat = "yyyy-MM-dd"
  63. //
  64. // let dateStr = formatter.string(from: self)
  65. // let nowStr = formatter.string(from: Date())
  66. // return dateStr == nowStr
  67. // }
  68. }
  69. extension Date {
  70. /// String -> Date
  71. ///
  72. /// - Parameters:
  73. /// - dateStr: date string
  74. /// - formatter: date formatter
  75. /// - Returns: Date
  76. static func date(_ dateStr: String, formatter: String = "yyyy-MM-dd HH:mm:ss") -> Date? {
  77. let dateFormatter = DateFormatter()
  78. dateFormatter.dateFormat = formatter
  79. dateFormatter.locale = Locale.current
  80. return dateFormatter.date(from: dateStr)
  81. }
  82. /// Date -> String
  83. ///
  84. /// - Parameter formatter: date formatter
  85. /// - Returns: date string
  86. func toString(_ formatter: String) -> String {
  87. let dateFormatter = DateFormatter()
  88. dateFormatter.dateFormat = formatter
  89. dateFormatter.locale = Locale.current
  90. return dateFormatter.string(from: self)
  91. }
  92. }
  93. extension Date {
  94. static func currentCalendar() -> Calendar {
  95. var sharedCalendar = Calendar(identifier: .gregorian)
  96. sharedCalendar.locale = Locale.current
  97. return sharedCalendar
  98. }
  99. /// Example: 2000/1/2 03:04:05 return 2000
  100. var year: Int {
  101. get {
  102. return Date.currentCalendar().component(.year, from: self)
  103. }
  104. }
  105. /// Example: 2000/1/2 03:04:05 return 1
  106. var month: Int {
  107. get {
  108. return Date.currentCalendar().component(.month, from: self)
  109. }
  110. }
  111. /// Example: 2000/1/2 03:04:05 return 2
  112. var day: Int {
  113. get {
  114. return Date.currentCalendar().component(.day, from: self)
  115. }
  116. }
  117. /// Example: 2000/1/2 03:04:05 return 3
  118. var hour: Int {
  119. get {
  120. return Date.currentCalendar().component(.hour, from: self)
  121. }
  122. }
  123. /// Example: 2000/1/2 03:04:05 return 4
  124. var minute: Int {
  125. get {
  126. return Date.currentCalendar().component(.minute, from: self)
  127. }
  128. }
  129. /// Example: 2000/1/2 03:04:05 return 5
  130. var second: Int {
  131. get {
  132. return Date.currentCalendar().component(.second, from: self)
  133. }
  134. }
  135. }
  136. extension Date {
  137. /// the same year
  138. ///
  139. /// - Parameter date: contrast time
  140. /// - Returns: true: equal; false: not equal
  141. func haveSameYear(_ date: Date) -> Bool {
  142. return self.year == date.year
  143. }
  144. func haveSameYearAndMonth(_ date: Date) -> Bool {
  145. return self.haveSameYear(date) && self.month == date.month
  146. }
  147. func haveSameYearMonthAndDay(_ date: Date) -> Bool {
  148. let components1 = Date.currentCalendar().dateComponents([.year, .month, .day], from: self)
  149. let components2 = Date.currentCalendar().dateComponents([.year, .month, .day], from: date)
  150. return components1 == components2
  151. }
  152. func haveSameYearMonthDayAndHour(_ date: Date) -> Bool {
  153. let components1 = Date.currentCalendar().dateComponents([.year, .month, .day, .hour], from: self)
  154. let components2 = Date.currentCalendar().dateComponents([.year, .month, .day, .hour], from: date)
  155. return components1 == components2
  156. }
  157. func haveSameYearMonthDayHourAndMinute(_ date: Date) -> Bool {
  158. let components1 = Date.currentCalendar().dateComponents([.year, .month, .day, .hour, .minute], from: self)
  159. let components2 = Date.currentCalendar().dateComponents([.year, .month, .day, .hour, .minute], from: date)
  160. return components1 == components2
  161. }
  162. func haveSameYearMonthDayHourMinuteAndSecond(_ date: Date) -> Bool {
  163. let components1 = Date.currentCalendar().dateComponents([.year, .month, .day, .hour, .minute, .second], from: self)
  164. let components2 = Date.currentCalendar().dateComponents([.year, .month, .day, .hour, .minute, .second], from: date)
  165. return components1 == components2
  166. }
  167. }
  168. extension Date {
  169. /// the number of days in the month
  170. ///
  171. /// - Returns: number of day
  172. func numberOfDaysInMonth() -> Int {
  173. if let range = Date.currentCalendar().range(of: .day, in: .month, for: self) {
  174. return range.count
  175. }
  176. return 0
  177. }
  178. }