DateInterval
有一个.contains
函数可以实现此功能(请参阅文档):
extension Date {
func isBetween(_ date1: Date, _ date2: Date) -> Bool {
date1 < date2
? DateInterval(start: date1, end: date2).contains(self)
: DateInterval(start: date2, end: date1).contains(self)
}
}
然后您可以像这样使用它:
let date = Date()
let date1 = Date(timeIntervalSinceNow: 1000)
let date2 = Date(timeIntervalSinceNow: -1000)
date.isBetween(date1, date2)
请注意,如果需要包括结束日期,则应该这样做,或者添加一个
guard
来捕获它:
guard self != max(date1, date2) || self == min(date1, date2) else { return false }
经过一些重构,你最终可能会得到类似这样的代码:
extension Date {
func isBetween(_ date1: Date, _ date2: Date) -> Bool {
let minDate = min(date1, date2)
let maxDate = max(date1, date2)
guard self != minDate else { return true }
guard self != maxDate else { return false }
return DateInterval(start: minDate, end: maxDate).contains(self)
}
}
那么这将通过以下测试用例:
XCTAssert(
try XCTUnwrap(Date(fromString: "2020/01/15 09:30")).isBetween(
try XCTUnwrap(Date(fromString: "2020/01/15 09:00")),
try XCTUnwrap(Date(fromString: "2020/01/15 10:00"))
)
)
XCTAssert(
try XCTUnwrap(Date(fromString: "2020/01/16 01:00")).isBetween(
try XCTUnwrap(Date(fromString: "2020/01/15 23:00")),
try XCTUnwrap(Date(fromString: "2020/01/16 04:00"))
)
)
XCTAssert(
try XCTUnwrap(Date(fromString: "2020/01/15 10:00")).isBetween(
try XCTUnwrap(Date(fromString: "2020/01/15 10:00")),
try XCTUnwrap(Date(fromString: "2020/01/15 10:30"))
)
)
XCTAssert(
try XCTUnwrap(Date(fromString: "2020/01/15 10:00")).isBetween(
try XCTUnwrap(Date(fromString: "2020/01/15 10:00")),
try XCTUnwrap(Date(fromString: "2020/01/15 10:00"))
)
)
XCTAssert(
try XCTUnwrap(Date(fromString: "2020/01/15 09:00")).isBetween(
try XCTUnwrap(Date(fromString: "2020/01/15 10:00")),
try XCTUnwrap(Date(fromString: "2020/01/15 08:00"))
)
)
XCTAssert(
try XCTUnwrap(Date(fromString: "2020/01/15 08:00")).isBetween(
try XCTUnwrap(Date(fromString: "2020/01/15 10:00")),
try XCTUnwrap(Date(fromString: "2020/01/15 08:00"))
)
)
XCTAssertFalse(
try XCTUnwrap(Date(fromString: "2020/01/15 09:00")).isBetween(
try XCTUnwrap(Date(fromString: "2020/01/15 10:00")),
try XCTUnwrap(Date(fromString: "2020/01/15 13:00"))
)
)
XCTAssertFalse(
try XCTUnwrap(Date(fromString: "2020/01/15 10:30")).isBetween(
try XCTUnwrap(Date(fromString: "2020/01/15 10:00")),
try XCTUnwrap(Date(fromString: "2020/01/15 10:30"))
)
)
XCTAssertFalse(
try XCTUnwrap(Date(fromString: "2020/01/15 10:00")).isBetween(
try XCTUnwrap(Date(fromString: "2020/01/15 10:00")),
try XCTUnwrap(Date(fromString: "2020/01/15 08:00"))
)
)
let date = Date()
let date1 = Date(timeIntervalSinceNow: 1000)
let date2 = Date(timeIntervalSinceNow: -1000)
XCTAssert(date.isBetween(date1, date2))
ClosedRange
和contains
语义意味着开始和结束日期都是包含在内的:(startDate...endDate).contains(startDate) == true
和(startDate...endDate).contains(endDate) == true
。 - Nikolai Ruheguard startDate < endDate else {...}
。 - jchmilewRange
类型由下限和上限组成,中间不包含任何元素。这与Python的range()
函数不同,后者会构建一个值数组。当边界类型为Date
时,甚至无法(或至少不明智)创建中间值:应该使用哪种分辨率(天?秒?纳秒?)。上述日期范围不是CountableRange
,因此无法迭代。 - Nikolai Ruhedate1...date2 ~= self
的意思是 "日期范围 date1 到 date2 是否包含了日期 self"。 - Roman Filippov