简体   繁体   English

日期不在开始日期和结束日期之内

[英]while a date is not within a start and end date swift

I have the following code which checks if a selectedDate is within a start and end date. 我有以下代码,用于检查selectedDate是否在开始日期和结束日期之内。 I want to know how to go about saying while the date is NOT within the 2 dates however I don't know how to go about it. 我想知道当日期不在2个日期之内时该如何说,但是我不知道该如何去做。 I tried using '!==' however it says 'Type of expression is ambiguous without more context' 我尝试使用'!=='但是它说'表达式的类型是模棱两可的,没有更多上下文

while newStartDate.compare(selectedDate) == .OrderedAscending && newEndDate.compare(selectedDate) == .OrderedDescending {
     print("\(selectedDate) is within \(newStartDate) & \(newEndDate)")     
}

What about ? 关于什么 ?

while newStartDate.compare(selectedDate) == .OrderedDescending || newEndDate.compare(selectedDate) == .OrderedAscending {
   print("\(selectedDate) is within \(newStartDate) & \(newEndDate)")
}

The selected date has to be before the start date or after the enddate. 所选日期必须在开始日期之前或结束日期之后。

I think JulianM's Answer is right. 我认为JulianM的答案是正确的。 In my opinion to make it more readable, you can use your first try with != : 我认为,使其更具可读性,您可以将第一次尝试使用!=

while newStartDate.compare(selectedDate) != .OrderedAscending || newEndDate.compare(selectedDate) != .OrderedDescending {
    print("\(selectedDate) is within \(newStartDate) & \(newEndDate)")
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM