Clicky

iOS Dev Nugget 149 Timepiece, a Swift Library for Handling Dates

.

Need to run a code review on your codebase? Hire me

Between NSDate, NSCalendar, NSDateFormatter and NSDateComponents, the iOS SDK provides a powerful system to operate on date and time values. The API however is a little verbose. Naoto Kaneko wrote a set of extensions for NSDate (and a few other types such as Int and String) in Swift, called Timepiece that makes date and time manipulate easier.

Creating new dates:

let now = NSDate()
let christmas = now.change(month: 12, day: 25)

Formatting:

5.minutes.later.stringFromFormat("yyyy-MM-dd HH:mm:SS")

Parsing:

"1987-06-02".dateFromFormat("yyyy-MM-dd")

Comparison:

date < 1.year.ago

There's a Swift playground included with it, so you can check it out easily. To use it, open Timepiece.xcworkspace and then open Timepiece.playground from within the workspace.

Sponsored: Learn how to make money selling your own apps. Start your app publishing business: go to LearnAppMaking.com and sign up!


Your feedback is valuable: Do you want more nuggets like this?   Yes   or   No

.

.

Like this and want such iOS dev nuggets to be emailed to you, weekly?

Sign Me Up! or follow @iosdevnuggets on Twitter

.

View archives of past issues