Skip to content

Instantly share code, notes, and snippets.

@miguellara
Created November 16, 2015 15:55
Show Gist options
  • Save miguellara/28a06578c88cc50b10c6 to your computer and use it in GitHub Desktop.
Save miguellara/28a06578c88cc50b10c6 to your computer and use it in GitHub Desktop.
Comparing NSDate
import Foundation
// MARK: - Extension
extension NSDate: Comparable {}
public func <(lhs: NSDate, rhs: NSDate) -> Bool {
return lhs.timeIntervalSinceReferenceDate < rhs.timeIntervalSinceReferenceDate
}
public func <=(lhs: NSDate, rhs: NSDate) -> Bool {
return lhs.timeIntervalSinceReferenceDate <= rhs.timeIntervalSinceReferenceDate
}
public func >=(lhs: NSDate, rhs: NSDate) -> Bool {
return lhs.timeIntervalSinceReferenceDate >= rhs.timeIntervalSinceReferenceDate
}
public func >(lhs: NSDate, rhs: NSDate) -> Bool {
return lhs.timeIntervalSinceReferenceDate > rhs.timeIntervalSinceReferenceDate
}
// MARK: - Examples
let now = NSDate()
let anHourAgo = now.dateByAddingTimeInterval(-60*60)
assert(now > anHourAgo)
assert(!(now < anHourAgo))
assert(!(now > now))
assert(!(now < now))
assert(now >= now)
assert(now <= now)
assert(now >= anHourAgo)
assert(!(now <= anHourAgo))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment