Skip to content

Instantly share code, notes, and snippets.

@psobot
Created June 23, 2015 04:08
Show Gist options
  • Star 2 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save psobot/d6f90cf4065770fde7ca to your computer and use it in GitHub Desktop.
Save psobot/d6f90cf4065770fde7ca to your computer and use it in GitHub Desktop.
Swift NSDate Comparison Extension
// 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