I want to know if two dates are from the same day!
Here’s a quick function to check if two dates are from the same day:
func isSameDay(date1: Date, date2: Date) -> Bool {
let diff = Calendar.current.dateComponents([.day], from: date1, to: date2)
if diff.day == 0 {
return true
} else {
return false
}
}
Of course you change the date components from .day
to .year
or .month
to check if the dates are from the same month or year.
func isSameDay(date1: Date, date2: Date) -> Bool {
let diff = Calendar.current.dateComponents([.month], from: date1, to: date2)
if diff.day == 0 {
return true
} else {
return false
}
}
func isSameDay(date1: Date, date2: Date) -> Bool {
let diff = Calendar.current.dateComponents([.year], from: date1, to: date2)
if diff.day == 0 {
return true
} else {
return false
}
}
The Complete iOS App Development Bootcamp
Disclosure: This website may contain affiliate links, meaning when you click the links and make a purchase, we receive a commission.