gpt4 book ai didi

swift - 检查两个日期之间的日期给出了错误的结果

转载 作者:行者123 更新时间:2023-11-28 05:40:31 25 4
gpt4 key购买 nike

<分区>

我想检查一个日期是否在两个日期之间,但是我的代码结果给我错误的结果,你能和我核实一下吗?感谢您的帮助!这是我的代码

extension Date{
func isBetweenDates(beginDate: Date, endDate: Date) -> Bool
{
if self.compare(beginDate) == .orderedAscending
{
return false
}

if self.compare(endDate) == .orderedDescending
{
return false
}

return true
}
}

override func viewDidLoad() {
let result0 = "08-01-2019".date(format: "dd-MM-YYYY")!.isBetweenDates(beginDate: "01-01-2019".date(format: "dd-MM-YYYY")!, endDate: "09-01-2019".date(format: "dd-MM-YYYY")!)

let result1 = "04-01-2019".date(format: "dd-MM-YYYY")!.isBetweenDates(beginDate: "06-01-2019".date(format: "dd-MM-YYYY")!, endDate: "08-01-2019".date(format: "dd-MM-YYYY")!)

let result2 = "05-01-2019".date(format: "dd-MM-YYYY")!.isBetweenDates(beginDate: "01-01-2019".date(format: "dd-MM-YYYY")!, endDate: "04-01-2019".date(format: "dd-MM-YYYY")!)

let result3 = "06-01-2018".date(format: "dd-MM-YYYY")!.isBetweenDates(beginDate: "01-01-2019".date(format: "dd-MM-YYYY")!, endDate: "08-01-2019".date(format: "dd-MM-YYYY")!)

}

和输出:result0 = true,result1 = true,result2 = true,result3 = false,或者它们应该是 result0 = true,result1 = false,result2 = false,result3 = false,

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com