From 462d73da2497ba0949e787a1e1955dc403c9ff2b Mon Sep 17 00:00:00 2001 From: Melvin Rivera Date: Thu, 26 May 2016 13:42:34 -0400 Subject: [PATCH] Added dateByAddingMonths, dateBySubtractingMonths, dateByAddingWeeks, dateBySubtractingWeeks --- AFDateHelper/AFDateExtension.swift | 52 ++++++++++++++++++++++++++++++ Demo/ViewController.swift | 16 +++++++++ README.md | 4 +++ 3 files changed, 72 insertions(+) diff --git a/AFDateHelper/AFDateExtension.swift b/AFDateHelper/AFDateExtension.swift index d9852c8..9efb436 100644 --- a/AFDateHelper/AFDateExtension.swift +++ b/AFDateHelper/AFDateExtension.swift @@ -337,6 +337,58 @@ public extension NSDate { // MARK: Adjusting Dates + /** + Creates a new date by a adding months. + + - Parameter days: The number of months to add. + - Returns A new date object. + */ + func dateByAddingMonths(months: Int) -> NSDate + { + let dateComp = NSDateComponents() + dateComp.month = months + return NSCalendar.currentCalendar().dateByAddingComponents(dateComp, toDate: self, options: NSCalendarOptions(rawValue: 0))! + } + + /** + Creates a new date by a substracting months. + + - Parameter days: The number of months to substract. + - Returns A new date object. + */ + func dateBySubtractingMonths(months: Int) -> NSDate + { + let dateComp = NSDateComponents() + dateComp.month = (months * -1) + return NSCalendar.currentCalendar().dateByAddingComponents(dateComp, toDate: self, options: NSCalendarOptions(rawValue: 0))! + } + + /** + Creates a new date by a adding weeks. + + - Parameter days: The number of weeks to add. + - Returns A new date object. + */ + func dateByAddingWeeks(weeks: Int) -> NSDate + { + let dateComp = NSDateComponents() + dateComp.day = 7 * weeks + return NSCalendar.currentCalendar().dateByAddingComponents(dateComp, toDate: self, options: NSCalendarOptions(rawValue: 0))! + } + + /** + Creates a new date by a substracting weeks. + + - Parameter days: The number of weeks to substract. + - Returns A new date object. + */ + func dateBySubtractingWeeks(weeks: Int) -> NSDate + { + let dateComp = NSDateComponents() + dateComp.day = ((7 * weeks) * -1) + return NSCalendar.currentCalendar().dateByAddingComponents(dateComp, toDate: self, options: NSCalendarOptions(rawValue: 0))! + } + /** Creates a new date by a adding days. diff --git a/Demo/ViewController.swift b/Demo/ViewController.swift index f3c8612..3146467 100644 --- a/Demo/ViewController.swift +++ b/Demo/ViewController.swift @@ -166,6 +166,22 @@ class ViewController: UITableViewController { sections.append("Adjusting Dates") sectionItems = [TableItem]() + // MARK: dateByAddingMonths + date = now.dateByAddingMonths(2) + sectionItems.append(TableItem(title: "Adding Months", description: "+ 2 Months: \(date.toString())")) + + // MARK: dateBySubstractingMonths + date = now.dateBySubtractingMonths(4) + sectionItems.append(TableItem(title: "Substracting Months", description: "- 4 Months: \(date.toString())")) + + // MARK: dateByAddingWeeks + date = now.dateByAddingWeeks(2) + sectionItems.append(TableItem(title: "Adding Weeks", description: "+ 2 Weeks: \(date.toString())")) + + // MARK: dateBySubstractingWeeks + date = now.dateBySubtractingWeeks(4) + sectionItems.append(TableItem(title: "Substracting Weeks", description: "- 4 Weeks: \(date.toString())")) + // MARK: dateByAddingDays date = now.dateByAddingDays(2) sectionItems.append(TableItem(title: "Adding Days", description: "+ 2 Days: \(date.toString())")) diff --git a/README.md b/README.md index c0c14ce..e243932 100644 --- a/README.md +++ b/README.md @@ -63,6 +63,10 @@ date.isLastYear() -> Bool ### Adjusting Dates ```Swift +date.dateByAddingMonths(2) -> NSDate +date.dateBySubtractingMonths(4) -> NSDate +date.dateByAddingWeeks(2) -> NSDate +date.dateBySubtractingWeeks(4) -> NSDate date.dateByAddingDays(2) -> NSDate date.dateBySubtractingDays(4) -> NSDate date.dateByAddingHours(2) -> NSDate