当日期不在开始和结束日期内时 swift
while a date is not within a start and end date swift
我有以下代码检查 selectedDate 是否在开始和结束日期内。我想知道当日期不在 2 个日期内时如何说,但我不知道如何去做。我尝试使用 '!==' 但它显示 'Type of expression is ambiguous without more context'
while newStartDate.compare(selectedDate) == .OrderedAscending && newEndDate.compare(selectedDate) == .OrderedDescending {
print("\(selectedDate) is within \(newStartDate) & \(newEndDate)")
}
那呢?
while newStartDate.compare(selectedDate) == .OrderedDescending || newEndDate.compare(selectedDate) == .OrderedAscending {
print("\(selectedDate) is within \(newStartDate) & \(newEndDate)")
}
所选日期必须在开始日期之前或结束日期之后。
我认为 JulianM 的 是正确的。在我看来,为了使其更具可读性,您可以使用 !=
:
进行第一次尝试
while newStartDate.compare(selectedDate) != .OrderedAscending || newEndDate.compare(selectedDate) != .OrderedDescending {
print("\(selectedDate) is within \(newStartDate) & \(newEndDate)")
}
我有以下代码检查 selectedDate 是否在开始和结束日期内。我想知道当日期不在 2 个日期内时如何说,但我不知道如何去做。我尝试使用 '!==' 但它显示 'Type of expression is ambiguous without more context'
while newStartDate.compare(selectedDate) == .OrderedAscending && newEndDate.compare(selectedDate) == .OrderedDescending {
print("\(selectedDate) is within \(newStartDate) & \(newEndDate)")
}
那呢?
while newStartDate.compare(selectedDate) == .OrderedDescending || newEndDate.compare(selectedDate) == .OrderedAscending {
print("\(selectedDate) is within \(newStartDate) & \(newEndDate)")
}
所选日期必须在开始日期之前或结束日期之后。
我认为 JulianM 的 !=
:
while newStartDate.compare(selectedDate) != .OrderedAscending || newEndDate.compare(selectedDate) != .OrderedDescending {
print("\(selectedDate) is within \(newStartDate) & \(newEndDate)")
}