Comparing Dates

I need to compare an attribute of Date data type with current date and check if it has a difference of 7 days or more.

I tried this but its not working:

if (Calendar.current.dateComponents([.day], from: resident.infectionDate ?? Date.distantPast, to: Date.now) < 7)

Error: Referencing operator function '<' on 'BinaryInteger' requires that 'DateComponents' conform to 'BinaryInteger'

Answered by Claude31 in 706350022

Try this:

var calendar = Calendar(identifier: .gregorian)
var days = calendar.dateComponents([.day], from: (resident.infectionDate ?? Date.distantPast), to: Date.now)
if days < 7 { }
Accepted Answer

Try this:

var calendar = Calendar(identifier: .gregorian)
var days = calendar.dateComponents([.day], from: (resident.infectionDate ?? Date.distantPast), to: Date.now)
if days < 7 { }
Comparing Dates
 
 
Q