Welcome to SwiftDate 3 a Date Management Library for Apple's platforms: the goal of this project is to allow developers to manage easily dates operation and timezones conversions in Swift. SwiftDate allows you to:
- Perform calculations with dates:
aDate + 2.weeks + 1.hoursor
(1.years - 2.hours + 16.minutes).fromNow()
- Compare dates with math operators
<,>,==,<=,>=. For example you can do
aDate1 >= aDate2or
- Easily get time components. E.g.
hour, minutes etc.
- Easy/optimized way to get and transform a date from and to strings (with relative date supports* like '2 hours, 5 minutes' etc.)
- Easy conversions to and from timezone, locale and calendar. Use helper class
DateInRegionand perform conversions with components and operations!
- Many shortcuts to get intervals and common dates (
- Compatible with Swift 2.0+ and iOS/Mac/WatchOS/tvOS platforms
- ... many many other shiny things!
NSDate is the central class of the date/time handling in Foundation framework. It encapsulates a moment in time that is internally stored as the number of seconds since Jan 1, 2001 at 00:00 UTC. It is universal as such that it is the same moment everywhere around the world and beyond (we do not have star date yet ;-) ). We call it 'absolute time'.
See absolute time as the moment that someone in the USA has a telephone conversation with someone in Dubai. Both have that conversation at the same moment (absolute time) but the local time will be different due to time zones, different calendars, different alphabets and/or different date notation methods. You need helper classes like
NSCalendar to represent an
NSDate object and perform calculations on it.
This important concept is the root of several problems for programmers when dealing with time conversions across time zones, calendars and locales in Cocoa. Here SwiftDate comes to the rescue.
It is time for SwiftDate
SwiftDate introduces the concepts of
DateInRegion. The former contains everything to represent a moment in time in the calendar, time zone and locale of the region specified. The latter is a combination of an absolute time (
NSDate) with a
DateRegion object. See it as an object that encapsulates a moment in time in a certain region.
In SwiftDate you can work with date components both for
When you work with
NSDate you are working with an absolute time and components are evaluated against your local region. When you work with the
DateInRegion class then all methods and properties are represented with a region that you can specify.
SwiftDate 1.2 branch last revision is available here. This version is unsupported.