extension Date {
/*
USE :日期之间相隔的天数
- fromeDate: 日期
- toDate: 截止日期
- 示例:Date.daysBetweenTwoDate...
*/
static func daysBetweenTwoDate_CN(fromeDate: Date, toDate: Date) -> Int {
let components = Calendar.current.dateComponents([.day], from: fromeDate, to: toDate)
return components.day ?? 0
}
}
let dfmatter = DateFormatter()
dfmatter.dateFormat="yyyy-MM-dd"
// 字符串转成时间
let endDate = dfmatter.date(from: model.expire_date)!
// 当前date
let now = Date().addingTimeInterval(TimeInterval(28800))
// 两个date间隔的天数
let days = Date.daysBetweenTwoDate_CN(fromeDate: now, toDate: endDate)
if days <= 7 && (now.compare(endDate) == .orderedAscending) {
}
/*
If:
a < b then return NSOrderedAscending. The left operand is smaller than the right operand.
a > b then return NSOrderedDescending. The left operand is greater than the right operand.
a == b then return NSOrderedSame. The operands are equal.
*/
public enum ComparisonResult : Int {
case orderedAscending // a < b
case orderedSame // a == b
case orderedDescending // a > b
}