Skip to content

Commit

Permalink
Merge pull request melvitax#14 from PiXeL16/StartOfMonth
Browse files Browse the repository at this point in the history
Add date at start of Month and date at end of Month
  • Loading branch information
melvitax committed Jul 31, 2015
2 parents 2223a63 + f36fa2f commit c0fe0bd
Show file tree
Hide file tree
Showing 2 changed files with 48 additions and 0 deletions.
40 changes: 40 additions & 0 deletions AFDateHelper/AFDateExtension.swift
Original file line number Diff line number Diff line change
Expand Up @@ -430,6 +430,44 @@ public extension NSDate {
}


/**
Return a new NSDate object of the first day of the month
:returns: NSDate
*/
func dateAtTheStartOfMonth() -> NSDate
{
//Create the date components
var components = self.components()
components.day = 1
//Builds the first day of the month
let firstDayOfMonthDate :NSDate = NSCalendar.currentCalendar().dateFromComponents(components)!

return firstDayOfMonthDate

}

/**
Return a new NSDate object of the last day of the month
:returns: NSDate
*/
func dateAtTheEndOfMonth() -> NSDate {

//Create the date components
var components = self.components()
//Set the last day of this month
components.month += 1
components.day = 0

//Builds the first day of the month
let lastDayOfMonth :NSDate = NSCalendar.currentCalendar().dateFromComponents(components)!

return lastDayOfMonth

}


// MARK: Retrieving Intervals

/**
Expand Down Expand Up @@ -594,6 +632,7 @@ public extension NSDate {
let interval: NSTimeInterval = self.timeIntervalSinceReferenceDate - distanceToStartOfWeek
return NSDate(timeIntervalSinceReferenceDate: interval).day()
}

/**
Returns the last day of the week.
Expand All @@ -605,6 +644,7 @@ public extension NSDate {
let interval: NSTimeInterval = self.timeIntervalSinceReferenceDate - distanceToStartOfWeek + distanceToEndOfWeek
return NSDate(timeIntervalSinceReferenceDate: interval).day()
}

/**
Checks to see if the date is a weekdday.
Expand Down
8 changes: 8 additions & 0 deletions Demo/ViewController.swift
Original file line number Diff line number Diff line change
Expand Up @@ -133,6 +133,14 @@ class ViewController: UITableViewController {
date = now.dateAtEndOfWeek()
sectionItems.append(TableItem(title: "End of Week", description: "\(date.toString())"))


date = now.dateAtTheStartOfMonth()
sectionItems.append(TableItem(title: "Start of Month", description: "\(date.toString())"))

date = now.dateAtTheEndOfMonth()
sectionItems.append(TableItem(title: "End of Month", description: "\(date.toString())"))


items.append(sectionItems)


Expand Down

0 comments on commit c0fe0bd

Please sign in to comment.