0

Hi everyone I'm trying to get a positive answer when comparing a date earlier than today

To do this I am using a Date extension with this boolean

extension Date {   
    var isPreviousDate: Bool {
        return self.timeIntervalSinceNow.sign == .minus 
    }
}

Here is my problem .. when I print the date to compare it tells me that today's date is earlier and I don't understand why

Since I'm having problems I tried to create a current date and compare it with today's date the answer is that today's date is earlier than today ... this is weird because it shouldn't tell me it's older

This is how I create the date to compare

    var calendar = Calendar(identifier: Calendar.current.identifier)
    calendar.timeZone = NSTimeZone(name: "UTC")! as TimeZone

    guard let selectedDate = calendar.date(from: DateComponents(year: Date().currentYear, month: Date().currentMonth, day: Date().currentDate)) else { return }

    print("isPrevious Date :",selectedDate.isPreviousDate)

    print(selectedDate)

am I doing something wrong?

this is what I read in the console when I print the created date to compare

isPrevious Date: true
2021-02-11 00:00:00 +0000
Tomas Jablonskis
  • 4,246
  • 4
  • 22
  • 40
kAiN
  • 2,559
  • 1
  • 26
  • 54
  • It is not clear what you are trying to achieve since you are talking about “today” but your code is using a time stamp. So is this a date comparison like “earlier than today” or is it a time comparison like “before right now/current time”? – Joakim Danielson Feb 14 '21 at 15:50
  • No as I said in the question I need to compare a specific date with the current date and know if the specific date is earlier than the current date. – kAiN Feb 14 '21 at 16:31

2 Answers2

1

timeIntervalSinceNow is a FloatingPoint value with a very high precision, that represents seconds passed since the Date.


To check if current date is at least on a previous day, you could do something like this:

extension Date {
    var isAtLeastPreviousDay: Bool {
        return isPast && !isToday
    }

    private var isPast: Bool {
        return self < Date()
    }

    private var isToday: Bool {
        return Calendar.current.isDateInToday(self)
    }    
}
Tomas Jablonskis
  • 4,246
  • 4
  • 22
  • 40
  • Thank you for your suggestion .. Apparently I was able to solve it by setting the boolean this way `self < Date() && !Calendar.current.isDateInToday(self)` – kAiN Feb 14 '21 at 16:33
  • @kAiN your issue was your calendar timezone set to UTC – Leo Dabus Feb 14 '21 at 23:42
1

The issue there is that you are using a custom calendar using UTC timezone. You should use the current calendar with the current timezone. Note that you can use Calendar method startOfDay to get the start of day of a specific date:

extension Date {
    var isPreviousDate: Bool {
        timeIntervalSinceNow.sign == .minus
    }
    var startOfDay: Date {
        Calendar.current.startOfDay(for: self)
    }
}

let now = Date()
let startOfDay = now.startOfDay
print("isPrevious Date:", startOfDay.isPreviousDate) // true

If you would like to check if a date is in yesterday all you need is to use calendar method isDateInYesterday:

extension Date {
    var isDateInYesterday: Bool {
        Calendar.current.isDateInYesterday(self)
    }
}

Date().isDateInYesterday                                 // false
Date(timeIntervalSinceNow: -3600*24).isDateInYesterday  // true
Leo Dabus
  • 229,809
  • 59
  • 489
  • 571