DateKit

Swift gave to developers great opportunity to learn new stuff. It was, and I believe still is, a refreshing breeze to programmers’ life. There’s also a great reason to show off – quickly learn the newest technology and prove your skills by releasing some great project. A lot of developers did that and it’s really great. Today I would like to present my attempt to simplify operations on NSDate and its components – DateKit.

Motivation

Everyone who worked with NSDateComponents knows how verbose it can be. Changing a single component involves an instance of NSCalendar, than an instance of NSDateComponents, then changing the component and finally you can get the result NSDate. It takes at least 3 lines of code that have to be written. It’s a bit disgusting. It makes mess in your code causing it hard to read and maintain in the future.

The idea behind DateKit was to encapsulate that operations in a single line of code. One line of code to modify components like seconds, minutes and hour should not be a problem. Comparing number of seconds or checking if only days of two dates are same should be very simple. I believe I achieved quite nice result. Keep on reading!

Features

Below you can find some cheat-sheet for DateKit. Every feature is presented in playground, so you can interact with it.

Modifying date components

You can modify one component at time:

  

or use chaining and change several in one line:

  

Dates comparison

Compare dates using operators:

  

or it’s components (compares Int values, so it doesn’t check if you compare same compoments):

  

or you can compare by chosen components like this:

  

compare(toDate:,byUnits:default) has default argument byUnit: which is [.CalendarUnitDay,.CalendarUnitMonth,.CalendarUnitYear] . This method returns NSComparisonResult , however you can determine bool value with shorthand methods like this:

  

Create new date by adding/substracting value to only one component

Get date from current date by changing one component with operators:

  

Create new date by adding/substracting values to several components (chaining)

Here’s an example of chaining:

  

Functions add(Int) and substract(Int) works like +/- math operators.

Helpers

I’ve added also some helpers. These are self-explaining:

  

Get the first and last unit in component:

  

Since today it’s January this code:

  

will return date with last day of February – 28th. If you want to get day itself, just add day at the end of chain:

  

It’s pretty simple, isn’t it?

Under the hood

There is no surprise under the hood – every operation is being made using NSCalendar instance and NSDateComponents . To achieve chaining I’ve introduced Operation object, which keeps date and NSCalendarUnit . If you create a chain, remember to return result date by adding date word at the end.

Summary

I hope DateKit will improve your work with dates. We are open for pull request with new features as well as bug reports. Code is available on Github. Happy operating on dates!

PS: I’m planning to add some new stuff to DateKit in the future, so contact me if you have any requests!