I have the current time, I need to check if the current time is between two times.
But I’m having trouble, as you can see startDate
and endDate
print past dates.
Can you give me a hand?
func getDate() -> Bool {
let start = "07:00"
let end = "19:00"
let dateFormat = "HH:mm"
let dateFormatter = DateFormatter()
dateFormatter.dateFormat = dateFormat
let startDate = dateFormatter.date(from: start)
let endDate = dateFormatter.date(from: end)
let currentDate = Date()
guard let startDate = startDate, let endDate = endDate else {
fatalError("Date Format does not match ⚠️")
}
print(startDate < currentDate && currentDate < endDate)
print(startDate) //2000-01-01 06:00:00 +0000
print(endDate) //2000-01-01 22:59:00 +0000
print(currentDate) //2021-07-13 22:11:05 +0000
return startDate < currentDate && currentDate < endDate
}
2
Answers
You can use
Calendar.current.date(bySetting...)
to set the hour/second/minute of an existing date. Then, compare those results.You just need to set your DateFormatter defaultDate to the start of the current date. If you would like to allow it to work with midnight (24:00) time as well you just need to set the date formatter isLenient to true. Note that if you create your date formatter inside your method it will create a new date formatter every time you call this method:
This will print:
Tuesday, July 13, 2021 at 11:00:00 PM Brasilia Standard Time
Wednesday, July 14, 2021 at 12:00:00 AM Brasilia Standard Time
Tuesday, July 13, 2021 at 11:42:02 PM Brasilia Standard Time