NSDate Comparison using Swift NSDate Comparison using Swift swift swift

NSDate Comparison using Swift


If you want to support ==, <, >, <=, or >= for NSDates, you just have to declare this somewhere:

public func ==(lhs: NSDate, rhs: NSDate) -> Bool {    return lhs === rhs || lhs.compare(rhs) == .OrderedSame}public func <(lhs: NSDate, rhs: NSDate) -> Bool {    return lhs.compare(rhs) == .OrderedAscending}extension NSDate: Comparable { }


I like using extensions to make code more readable. Here are a few NSDate extensions that can help clean your code up and make it easy to understand. I put this in a sharedCode.swift file:

extension NSDate {    func isGreaterThanDate(dateToCompare: NSDate) -> Bool {        //Declare Variables        var isGreater = false        //Compare Values        if self.compare(dateToCompare as Date) == ComparisonResult.orderedDescending {            isGreater = true        }        //Return Result        return isGreater    }    func isLessThanDate(dateToCompare: NSDate) -> Bool {        //Declare Variables        var isLess = false        //Compare Values        if self.compare(dateToCompare as Date) == ComparisonResult.orderedAscending {            isLess = true        }        //Return Result        return isLess    }    func equalToDate(dateToCompare: NSDate) -> Bool {        //Declare Variables        var isEqualTo = false        //Compare Values        if self.compare(dateToCompare as Date) == ComparisonResult.orderedSame {            isEqualTo = true        }        //Return Result        return isEqualTo    }    func addDays(daysToAdd: Int) -> NSDate {        let secondsInDays: TimeInterval = Double(daysToAdd) * 60 * 60 * 24        let dateWithDaysAdded: NSDate = self.addingTimeInterval(secondsInDays)        //Return Result        return dateWithDaysAdded    }    func addHours(hoursToAdd: Int) -> NSDate {        let secondsInHours: TimeInterval = Double(hoursToAdd) * 60 * 60        let dateWithHoursAdded: NSDate = self.addingTimeInterval(secondsInHours)        //Return Result        return dateWithHoursAdded    }}

Now if you can do something like this:

//Get Current Date/Timevar currentDateTime = NSDate()//Get Reminder Date (which is Due date minus 7 days lets say)var reminderDate = dueDate.addDays(-7)//Check if reminderDate is Greater than Right nowif(reminderDate.isGreaterThanDate(currentDateTime)) {    //Do Something...}


This is how you compare two NSDates in Swift, I just tested it in Xcode's playground:

if date1.compare(date2) == NSComparisonResult.OrderedDescending{    NSLog("date1 after date2");} else if date1.compare(date2) == NSComparisonResult.OrderedAscending{    NSLog("date1 before date2");} else{    NSLog("dates are equal");}

So to check if a date dueDate is within a week from now:

let dueDate=...let calendar = NSCalendar.currentCalendar()let comps = NSDateComponents()comps.day = 7let date2 = calendar.dateByAddingComponents(comps, toDate: NSDate(), options: NSCalendarOptions.allZeros)if dueDate.compare(date2!) == NSComparisonResult.OrderedDescending{    NSLog("not due within a week");} else if dueDate.compare(date2!) == NSComparisonResult.OrderedAscending{    NSLog("due within a week");} else{    NSLog("due in exactly a week (to the second, this will rarely happen in practice)");}