Tunis is an easy to use library for performing date and time calculations in iOS. Tunits allows you to quickly create an array of all the days in a given month or find the first day of the next month.
Have comments or suggestsions? Feel free to submit a pull request or find me on Twitter (@spitzgoby)
Requires XCode 6 and iOS 9+ sdk.
Tunits is available through CocoaPods. To install it, simply add the following line to your Podfile:
pod "Tunits"
To run the example project, clone the repo, and run pod install
from the Example directory first.
TimeUnit
methods can be used by creating TimeUnit
instances.
The NSCalendar.currentCalendar()
is used by default, but TimeUnit
instances expose their calendar property so a custom calendar can be
set.
// Instance
let tunit = TimeUnit() // init new TimeUnit object
let daysOfCurrentMonth = tunit.daysOfMonth(NSDate()) // create array of days
// Custom Calendar
let calendar = NSCalendar.currentCalendar()
calendar.firstWeekday = 2 // set first weekday to Monday
let tunit = TimeUnit()
tunit.calendar = calendar
let daysOfCurrentWeek = tunit.daysOfWeek(NSDate())
TimeUnit
also has static versions of its instance methods. This makes code
cleaner, but the NSCalendar.currentCalendar()
will be used for all
calculations.
// Static
let daysOfCurrentMonth = TimeUnit.daysOfMonth(NSDate()) // create array of days
In addition to using TimeUnit
methods directly, NSDate
objects have
been extended to use the TimeUnit
methods that return dates. This
means methods can be chained together for complex date selection.
// Method chaining
let endOfNextYear = NSDate().yearAfter().endOfYear() // Last second of next year
Thomas Leu, tomleu@gmail.com
Tunits is available under the MIT license. See the LICENSE file for more info.