How can I check if any date is between two dates? For instance I want to check if current date is between 15 December and 15 January.
This can be any year so my current date could be 15 December 2023 and it should return true. If it's 14 of December or 16 January any year it should return false. 31 December should return true.
This is what I tried:
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
But it seams a bit buggy because if my time zone is UTC 2 then the end date gives me 30 December 22:00:00 because UTC 2 is 31 December 00:00 - 2 hours it gives you 30 December instead of 31.
Ideally I would like to not have to separate checks for dates and just have one inclusive check between 15-december and 15 January rather than check 15-december - 31 December and 1 janury to 7 - January.
CodePudding user response:
You cannot ignore the year especially if the turn of the year is within the interval. And as the current date is involved practically you want to check if the date is within December of the current year and Januar of the next year
My suggestion is to get the year component from the current date, then build the start date by setting month to 12 and day to 15 and the end date by adding one day and one month to the start date which points to Jan 16 in the next year.
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")
}
CodePudding user response:
I would get the date components for month and day for the date and do a simple comparison of the values
let components = Calendar.current.dateComponents([.month, .day], from: date)
let inPeriod = (components.month! == 12 && components.day! >= 15) || (components.month! == 1 && components.day! <= 15)
Simple test case
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)!
}
CodePudding user response:
Date conforms with Comparable protocol, so you can just:
var initialDate = Date(timeIntervalSince1970: 10000)
var finalDate = Date(timeIntervalSinceNow: 10000)
var todayDate = Date()
todayDate > initialDate
todayDate < finalDate
Use in Ifs or whatever you like.