Skip to content

Instantly share code, notes, and snippets.

@calebhicks
Created October 21, 2015 17:13
Show Gist options
  • Save calebhicks/4c173907948c8957c8bf to your computer and use it in GitHub Desktop.
Save calebhicks/4c173907948c8957c8bf to your computer and use it in GitHub Desktop.
Swift Date Helpers
extension NSDate
{
func isGreaterThanDate(dateToCompare : NSDate) -> Bool
{
//Declare Variables
var isGreater = false
//Compare Values
if self.compare(dateToCompare) == NSComparisonResult.OrderedDescending
{
isGreater = true
}
//Return Result
return isGreater
}
func isLessThanDate(dateToCompare : NSDate) -> Bool
{
//Declare Variables
var isLess = false
//Compare Values
if self.compare(dateToCompare) == NSComparisonResult.OrderedAscending
{
isLess = true
}
//Return Result
return isLess
}
func isEqualToDate(dateToCompare : NSDate) -> Bool
{
//Declare Variables
var isEqualTo = false
//Compare Values
if self.compare(dateToCompare) == NSComparisonResult.OrderedSame
{
isEqualTo = true
}
//Return Result
return isEqualTo
}
func addDays(daysToAdd : Int) -> NSDate
{
var secondsInDays : NSTimeInterval = Double(daysToAdd) * 60 * 60 * 24
var dateWithDaysAdded : NSDate = self.dateByAddingTimeInterval(secondsInDays)
//Return Result
return dateWithDaysAdded
}
func addHours(hoursToAdd : Int) -> NSDate
{
var secondsInHours : NSTimeInterval = Double(hoursToAdd) * 60 * 60
var dateWithHoursAdded : NSDate = self.dateByAddingTimeInterval(secondsInHours)
//Return Result
return dateWithHoursAdded
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment