Created
June 23, 2015 04:08
-
-
Save psobot/d6f90cf4065770fde7ca to your computer and use it in GitHub Desktop.
Swift NSDate Comparison Extension
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// NSDate doesn't include overrides for standard comparison operators in Swift. | |
// This extension adds <, >, <=, >=, and ==, using NSDate's built-in `compare` method. | |
// MIT licensed. | |
func <=(lhs: NSDate, rhs: NSDate) -> Bool { | |
let res = lhs.compare(rhs) | |
return res == .OrderedAscending || res == .OrderedSame | |
} | |
func >=(lhs: NSDate, rhs: NSDate) -> Bool { | |
let res = lhs.compare(rhs) | |
return res == .OrderedDescending || res == .OrderedSame | |
} | |
func >(lhs: NSDate, rhs: NSDate) -> Bool { | |
return lhs.compare(rhs) == .OrderedDescending | |
} | |
func <(lhs: NSDate, rhs: NSDate) -> Bool { | |
return lhs.compare(rhs) == .OrderedAscending | |
} | |
func ==(lhs: NSDate, rhs: NSDate) -> Bool { | |
return lhs.compare(rhs) == .OrderedSame | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment