如何檢查兩個日期之間是否有任何日期?例如,我想檢查當前日期是否在 12 月 15 日到 1 月 15 日之間。
這可以是任何一年,所以我當前的日期可能是 2023 年 12 月 15 日,它應該回傳 true。如果是任何一年的 12 月 14 日或 1 月 16 日,它應該回傳 false。12 月 31 日應該回傳 true。
這是我嘗試過的:
let now = currentDateProvider()
dateFormatter.dateFormat = "yyyy-MM-dd"
let year = Calendar.current.component(.year, from: now)
guard let start = dateFormatter.date(from: "\(year)-01-01"),
let end = dateFormatter.date(from: "\(year)-01-07") else {
return false
}
if start <= now && now <= end {
return true
}
guard let start = dateFormatter.date(from: "\(year)-12-01"),
let end = dateFormatter.date(from: "\(year)-12-31") else {
return false
}
let value = start <= now && now <= end
return value
但它接縫有點錯誤,因為如果我的時區是 UTC 2,那么結束日期給我 12 月 30 日 22:00:00,因為 UTC 2 是 12 月 31 日 00:00 - 2 小時它給你 12 月 30 日而不是 31 日。
理想情況下,我希望不必單獨檢查日期,只需在 12 月 15 日至 1 月 15 日之間進行一次包容性檢查,而不是檢查 12 月 15 日至 12 月 31 日和 1 月 1 日至 1 月 7 日。
uj5u.com熱心網友回復:
我將獲取日期的月份和日期的日期組件,并對值進行簡單比較
let components = Calendar.current.dateComponents([.month, .day], from: date)
let inPeriod = (components.month! == 12 && components.day! >= 15) || (components.month! == 1 && components.day! <= 15)
簡單的測驗用例
var testDate = Calendar.current.date(from: DateComponents(calendar: .current, year: 2021, month: 12, day: 14))!
for _ in 1...34 {
let components = Calendar.current.dateComponents([.month, .day], from: testDate)
print(testDate, (components.month! == 12 && components.day! >= 15) || (components.month! == 1 && components.day! <= 15))
testDate = Calendar.current.date(byAdding: .day, value: 1, to: testDate)!
}
uj5u.com熱心網友回復:
您不能忽略年份,尤其是當年份在間隔內時。由于實際上涉及當前日期,因此您要檢查日期是否在當年的 12 月和明年的1 月之內
我的建議是從當前日期獲取年份組件,然后通過將月份設定為 12 并將天設定為 15 來構建開始日期,并通過在指向下一個 1 月 16 日的開始日期添加一天和一個月來構建結束日期年。
let currentDate = Date.now
let year = Calendar.current.component(.year, from: currentDate)
let startComponents = DateComponents(year: year, month: 12, day: 15)
let startDate = Calendar.current.date(from: startComponents)!
let endDate = Calendar.current.date(byAdding: DateComponents(month: 1, day: 1), to: startDate)!
if currentDate >= startDate && currentDate < endDate {
print("isValid")
}
uj5u.com熱心網友回復:
我要贖回自己!
沒有看到您正在尋找考慮到您的時區的解決方案
沒有看到你想要的東西只對間隔感興趣,而不是對年份感興趣。
我必須承認,有時我閱讀時沒有注意問題,特別是那些我認為很容易的問題。
希望下面的解決方案能滿足您的需求。正如其他學院所說,你不可能不考慮年份,但你可以像你一樣在函式內部模擬年份。
尋找自己,看看您是否更喜歡我的解決方案或堅持您的解決方案:
import Foundation
let f = ISO8601DateFormatter()
f.formatOptions = .withFullDate
f.timeZone = TimeZone(abbreviation: "PST")
func thisDate(_ date: Date, isBetweenthisMonth initialMonth: Int, andDay initialDay: Int, andThisOtherMonth finalMonth: Int, andThisOtherDay finalDay: Int) -> Bool {
var isBetween = false
let year = Calendar.current.component(.year, from: date)
let initialYear = year
var finalYear = year
if finalMonth < initialMonth {
finalYear = finalYear
}
let initialFakeData = f.date(from: "\(initialYear)-\(initialMonth)-\(initialDay)")!
let finalFakeData = f.date(from: "\(finalYear)-\(finalMonth)-\(finalDay)")!
if (date > initialFakeData && date < finalFakeData) {
isBetween = true
}
return isBetween
}
// Some test cases to pass
let date = f.date(from: "2022-11-01")!
print(thisDate(date, isBetweenthisMonth: 12, andDay: 02, andThisOtherMonth: 02, andThisOtherDay: 23) == false ? "False" : "Something wrong.")
print(thisDate(date, isBetweenthisMonth: 10, andDay: 02, andThisOtherMonth: 01, andThisOtherDay: 15) == true ? "True" : "Something wrong.")
let date2 = f.date(from: "2022-12-31")!
print(thisDate(date2, isBetweenthisMonth: 12, andDay: 30, andThisOtherMonth: 01, andThisOtherDay: 01) == true ? "True" : "Something wrong.")
print(thisDate(date2, isBetweenthisMonth: 01, andDay: 01, andThisOtherMonth: 01, andThisOtherDay: 15) == false ? "False" : "Something wrong.")
let date_now = f.date(from: f.string(from: Date()))!
print(thisDate(date_now, isBetweenthisMonth: 11, andDay: 08, andThisOtherMonth: 11, andThisOtherDay: 10) == true ? "True" : "Something wrong.")
print(thisDate(date_now, isBetweenthisMonth: 12, andDay: 01, andThisOtherMonth: 02, andThisOtherDay: 01) == false ? "False" : "Something wrong.")
我不知道如何在 Playground 中使用 XCTest,所以我在最終代碼中模擬了一些測驗。
轉載請註明出處,本文鏈接:https://www.uj5u.com/qita/532281.html
標籤:迅速日期
上一篇:用R中的NA替換某些日期
