Created
November 16, 2015 15:55
-
-
Save miguellara/28a06578c88cc50b10c6 to your computer and use it in GitHub Desktop.
Comparing NSDate
This file contains 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
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