Skip to content

Commit

Permalink
Added dateByAddingMonths, dateBySubtractingMonths, dateByAddingWeeks,…
Browse files Browse the repository at this point in the history
… dateBySubtractingWeeks
  • Loading branch information
melvitax committed May 26, 2016
1 parent d84ea41 commit 462d73d
Show file tree
Hide file tree
Showing 3 changed files with 72 additions and 0 deletions.
52 changes: 52 additions & 0 deletions AFDateHelper/AFDateExtension.swift
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down
16 changes: 16 additions & 0 deletions Demo/ViewController.swift
Original file line number Diff line number Diff line change
Expand Up @@ -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())"))
Expand Down
4 changes: 4 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down

0 comments on commit 462d73d

Please sign in to comment.