Skip to content

Instantly share code, notes, and snippets.

@vikingosegundo
Last active November 13, 2018 22:30
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save vikingosegundo/31ddb14920415ef444a9ab550411d4ff to your computer and use it in GitHub Desktop.
Save vikingosegundo/31ddb14920415ef444a9ab550411d4ff to your computer and use it in GitHub Desktop.
import Foundation
extension Date {
func add(years: Int = 0, months: Int = 0, days: Int = 0, hours: Int = 0, minutes: Int = 0, seconds: Int = 0) -> Date? {
let comp = DateComponents(year: years, month: months, day: days, hour: hours, minute: minutes, second: seconds)
return Calendar.current.date(byAdding: comp, to: self)
}
func subtract(years: Int = 0, months: Int = 0, days: Int = 0, hours: Int = 0, minutes: Int = 0, seconds: Int = 0) -> Date? {
return add(years: -years, months: -months, days: -days, hours: -hours, minutes: -minutes, seconds: -seconds)
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment