Skip to content

Instantly share code, notes, and snippets.

@gregszero
Created January 31, 2018 15:17
Show Gist options
  • Save gregszero/8a01e6b5886efb811b0eb0af3006c40e to your computer and use it in GitHub Desktop.
Save gregszero/8a01e6b5886efb811b0eb0af3006c40e to your computer and use it in GitHub Desktop.
Easy to use add and remove days to a Date
extension Date {
/// Returns a Date with the specified days added to the one it is called with
func add(years: Int = 0, months: Int = 0, days: Int = 0, hours: Int = 0, minutes: Int = 0, seconds: Int = 0) -> Date {
var targetDay: Date
targetDay = Calendar.current.date(byAdding: .year, value: years, to: self)!
targetDay = Calendar.current.date(byAdding: .month, value: months, to: targetDay)!
targetDay = Calendar.current.date(byAdding: .day, value: days, to: targetDay)!
targetDay = Calendar.current.date(byAdding: .hour, value: hours, to: targetDay)!
targetDay = Calendar.current.date(byAdding: .minute, value: minutes, to: targetDay)!
targetDay = Calendar.current.date(byAdding: .second, value: seconds, to: targetDay)!
return targetDay
}
/// Returns a Date with the specified days subtracted from the one it is called with
func subtract(years: Int = 0, months: Int = 0, days: Int = 0, hours: Int = 0, minutes: Int = 0, seconds: Int = 0) -> Date {
let inverseYears = -1 * years
let inverseMonths = -1 * months
let inverseDays = -1 * days
let inverseHours = -1 * hours
let inverseMinutes = -1 * minutes
let inverseSeconds = -1 * seconds
return add(years: inverseYears, months: inverseMonths, days: inverseDays, hours: inverseHours, minutes: inverseMinutes, seconds: inverseSeconds)
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment