Skip to content

Commit

Permalink
Added dateByAddingSeconds and dateBySubtractingSeconds
Browse files Browse the repository at this point in the history
  • Loading branch information
melvitax committed Dec 4, 2015
1 parent a6e5ca7 commit 401b15a
Show file tree
Hide file tree
Showing 2 changed files with 35 additions and 1 deletion.
28 changes: 27 additions & 1 deletion AFDateHelper/AFDateExtension.swift
Original file line number Diff line number Diff line change
Expand Up @@ -405,7 +405,7 @@ public extension NSDate {
}

/**
Returns a new NSDate object by a adding minutes.
Returns a new NSDate object by a substracting minutes.
:param: days :Int Minutes to add.
:returns: NSDate
Expand All @@ -417,6 +417,32 @@ public extension NSDate {
return NSCalendar.currentCalendar().dateByAddingComponents(dateComp, toDate: self, options: NSCalendarOptions(rawValue: 0))!
}

/**
Returns a new NSDate object by a adding seconds.
:param: Seconds Seconds to add.
:returns: NSDate
*/
func dateByAddingSeconds(seconds: Int) -> NSDate
{
let dateComp = NSDateComponents()
dateComp.second = seconds
return NSCalendar.currentCalendar().dateByAddingComponents(dateComp, toDate: self, options: NSCalendarOptions(rawValue: 0))!
}

/**
Returns a new NSDate object by a substracting seconds.
:param: days :Int Seconds to substract.
:returns: NSDate
*/
func dateBySubtractingSeconds(seconds: Int) -> NSDate
{
let dateComp = NSDateComponents()
dateComp.second = (seconds * -1)
return NSCalendar.currentCalendar().dateByAddingComponents(dateComp, toDate: self, options: NSCalendarOptions(rawValue: 0))!
}

/**
Returns a new NSDate object from the start of the day.
Expand Down
8 changes: 8 additions & 0 deletions Demo/ViewController.swift
Original file line number Diff line number Diff line change
Expand Up @@ -181,6 +181,14 @@ class ViewController: UITableViewController {
date = now.dateBySubtractingMinutes(4)
sectionItems.append(TableItem(title: "Substracting Minutes", description: "- 4 Minutes: \(date.toString())"))

// MARK: dateByAddingSeconds
date = now.dateByAddingSeconds(90)
sectionItems.append(TableItem(title: "Adding Seconds", description: "+ 90 Seconds: \(date.toString())"))

// MARK: dateBySubstractingSeconds
date = now.dateBySubtractingSeconds(90)
sectionItems.append(TableItem(title: "Substracting Seconds", description: "- 90 Minutes: \(date.toString())"))

// MARK: dateAtStartOfDay
date = now.dateAtStartOfDay()
sectionItems.append(TableItem(title: "Start of Day", description: "\(date.toString())"))
Expand Down

0 comments on commit 401b15a

Please sign in to comment.