CVCalendarKit is a wrapper around NSDate which provides a convenience way for dealing with dates through overloaded operators and extensions.
Features
Currently available features.
- DateUnit operations (year, month, day) – multiplication, division, addition, subtraction
- Trailing operations
- NSDate comparison
- NSDate convenience stuff
- Date construction with custom values
- Date construction with String
- Date unit values
- Weekdays
Further changes.
- NSCalendar wrapper
- Simplifyig the process of working with events
- Reading/Writing calendar data
Setup
* Swift 1.2 * iOS 7 or higher Requirements.
CocoaPods.
pod 'CVCalendarKit', '~> 0.1.5'
Manual setup.
Download CVCalendarKit project source code and add CVCalendarKit folder into your target (copy if needed).
Usage
Using CVCalendarKit is extremely easy and intuitive. All you need is an instance of a NSDate.
/// Current date.
let today = NSDate()
Now you can use addition and substraction on a DateUnit.
/// Day operations.
let tomorrow = today.day + 1
let yesterday = today.day - 1
/// Month operations.
let monthAhead = today.month + 1
let monthAgo = today.month - 1
/// Year operations.
let yearAhead = today.year + 1
let yearAgo = today.year - 1
Or even crazier operations!
/// Multiplication and Division.
let multipliedDate = today.day * 5
let dividedDate = today.month / 5
You can get really difficult calculations with trailing.
/// Trailing operations.
let dayAheadMonthAgo = (today.day + 1).month - 1
let yearAheadTwoMonthsAgoFiveDaysAhead = ((today.year + 1).month - 2).day + 5
NSDate String
description.
/// Date description.
let date = (NSDate().month == 5).descriptionWithLocale(nil, format: .DDMMYY, style: nil)
Constructing a date from its description String
.
/// Date from String (its description).
if let myDate = "May 21, 1997".date(.DDMMYY, style: .MediumStyle) {
// Further stuff...
}
Do you need to compare NSDate instances? Here you go!
/// NSDate comparison.
today == yesterday
today > yesterday
yesterday <= tomorrow
tomorrow != today
You can also get the first/last date in your date's month/year.
/// Convenience stuff.
let firstDateInCurrentMonth = today.firstMonthDate()
let lastDateInCurrentMonth = today.lastMonthDate()
let firstDateInCurrentYear = today.firstYearDate()
let lastDateInCurrentYear = today.lastYearDate()
Custom date can be constructed using ==
overloaded operator on NSDate objects.
/// Date construction by assigning values.
let customDate = ((today.day == 21).month == 5).year == 1997
Accessing date units' values.
/// Date unit values (year, month, day).
let todaysYear = today.year.value()
let todaysMonth = today.month.value()
let todaysDay = today.day.value()
And getting weekday enum/raw value.
/// NSDate+Weekday.
let todaysWeekday = today.weekday // Enum value.
let todaysWeekdayRaw = today.weekday.rawValue // Raw value.
Acknowledgments
- Author Eugene Mozharovsky
- Inspired by Timepiece and SwiftMoment
License
CVCalendarKit is released under the MIT license. For more information see the LICENSE file.