DateUtils.swift 5.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137
  1. //
  2. // DateUtils.swift
  3. // MTP2_iOS
  4. //
  5. // Created by zhongyuan on 2018/6/12.
  6. // Copyright © 2018年 zhongyuan.All rights reserved.
  7. //
  8. import Foundation
  9. /// 日期工具类
  10. class DateUtils {
  11. /// 获取指定时间上一个月的时间
  12. ///
  13. /// - Parameter date: 不填时获取当前时间
  14. /// - Returns: 上一个月的时间
  15. static func getLastDate(_ date: Date = Date()) -> Date {
  16. var comps = DateComponents()
  17. comps.month = -1
  18. let calender = Calendar(identifier: .gregorian)
  19. let mdate = calender.date(byAdding: comps, to: date)
  20. return mdate!
  21. }
  22. /// 获取任一月份的时间
  23. /// - Parameters:
  24. /// - month: month
  25. /// - date: date
  26. static func getDiffMonthDate(month: Int, _ date: Date = Date()) -> Date {
  27. var comps = DateComponents()
  28. comps.month = month
  29. let calender = Calendar(identifier: .gregorian)
  30. let mdate = calender.date(byAdding: comps, to: date)
  31. return mdate!
  32. }
  33. /// 获取任一天数的时间
  34. /// - Parameters:
  35. /// - month: month
  36. /// - date: date
  37. static func getDiffDayhDate(day: Int, _ date: Date = Date()) -> Date {
  38. var comps = DateComponents()
  39. comps.day = day
  40. let calender = Calendar(identifier: .gregorian)
  41. let mdate = calender.date(byAdding: comps, to: date)
  42. return mdate!
  43. }
  44. static func getNextMonthDate(_ date: Date = Date()) -> Date {
  45. var comps = DateComponents()
  46. comps.month = 1
  47. let calender = Calendar(identifier: .gregorian)
  48. let mdate = calender.date(byAdding: comps, to: date)
  49. return mdate!
  50. }
  51. static func getSevenDaysAgo(_ date: Date = Date()) -> Date {
  52. var comps = DateComponents()
  53. comps.day = -7
  54. let calender = Calendar(identifier: .gregorian)
  55. let mdate = calender.date(byAdding: comps, to: date)
  56. return mdate!
  57. }
  58. /// 获取本年开始日期的方法
  59. static func startOfCurrentYear() -> Date {
  60. let date = Date()
  61. let calendar = Calendar.current
  62. let components = calendar.dateComponents(Set<Calendar.Component>([.year]), from: date)
  63. let startOfYear = calendar.date(from: components)!
  64. return startOfYear
  65. }
  66. /// 获取本月开始日期的方法
  67. static func startOfCurrentMonth() -> Date {
  68. let date = Date()
  69. let calendar = Calendar.current
  70. let components = calendar.dateComponents(Set<Calendar.Component>([.year, .month]), from: date)
  71. let startOfYear = calendar.date(from: components)!
  72. return startOfYear
  73. }
  74. /// 判断两个日期是否在同年的同一个月
  75. static func judgeSameYearMonth(target: Date, source: Date = Date()) -> Bool {
  76. let calendar = Calendar.current
  77. let targetComponents = calendar.dateComponents(Set<Calendar.Component>([.year, .month]), from: target)
  78. let sourceComponents = calendar.dateComponents(Set<Calendar.Component>([.year, .month]), from: source)
  79. if calendar.date(from: targetComponents)!.compare(calendar.date(from: sourceComponents)!) == .orderedSame {
  80. return true
  81. }
  82. return false
  83. }
  84. /// 根据时间戳获取对应的时间信息
  85. /// - Parameters:
  86. /// - timeInterval: timeInterval
  87. /// - formatter: formatter
  88. /// - Returns: String
  89. static func dateStringSince1970(_ timeInterval: TimeInterval, _ formatter: String = "yyyy-MM-dd") -> String {
  90. return Date(timeIntervalSince1970: timeInterval/1000).getString(formatter: formatter)
  91. }
  92. /// 获取带T的时间戳的时间字符串
  93. /// - Parameter dateString: dateString
  94. /// - Returns: String
  95. static func getTDateString(_ dateString: String, _ formatter: String = "yyyy-MM-dd") -> String {
  96. let dateFormatter = DateFormatter()
  97. let tempLocale = dateFormatter.locale
  98. dateFormatter.locale = Locale(identifier: "en_US_POSIX")
  99. dateFormatter.dateFormat = "yyyy-MM-dd'T'HH:mm:ssZ"
  100. let date = dateFormatter.date(from: dateString) ?? Date()
  101. dateFormatter.dateFormat = formatter
  102. dateFormatter.locale = tempLocale
  103. return dateFormatter.string(from: date)
  104. }
  105. /// 获取两个时间戳间隔时分秒
  106. /// - Parameters:
  107. /// - timer1: timer1
  108. /// - timer2: timer2
  109. /// - count: count
  110. /// - Returns: String
  111. static func diffs(_ timer1: TimeInterval, _ timer2: TimeInterval, _ count: Double) -> DateComponents? {
  112. let formatter = DateFormatter()
  113. formatter.dateStyle = .medium
  114. formatter.timeStyle = .short
  115. formatter.dateFormat = "YYYY-MM-dd HH:mm:ss"
  116. let date1 = Date(timeIntervalSince1970: timer1-count)
  117. let date2 = Date(timeIntervalSince1970: timer2)
  118. /// 日历对象(方便比较两个日期之间的差距)
  119. let calendar = Calendar.current
  120. return calendar.dateComponents(Set<Calendar.Component>([.hour, .minute, .second]), from: date1, to: date2)
  121. }
  122. }