Swift - How to check if an NSDate is yesterday compare to current time?

As of iOS 8.0, you can use -[NSCalendar isDateInYesterday:], like this:

let calendar = NSCalendar.autoupdatingCurrentCalendar()

let someDate: NSDate = some date...
if calendar.isDateInYesterday(someDate) {
    // It was yesterday...
}

If you'll be doing this a lot, you should create the calendar once and keep it in an instance variable, because creating the calendar object is not trivial.


In Swift 3/4/5 the API has changed:

Calendar.current.isDateInToday(yourDate)
Calendar.current.isDateInYesterday(yourDate)
Calendar.current.isDateInTomorrow(yourDate)

To get the current date:

let now = Date()