Extensions
Overview
As you know one of the main features of Swift is to be type safe, which basically means that you can’t use a variable inappropriately for the terms of its type. In other words, according to the rules of type safety, you can divide by zero — that’s not a type error — but you can’t perform an arithmetic operation with a string unless you do some conversions first.
In order to make a more Swift-like environment lots of Foundation classes are now typesafe since Swift3.
SwiftDate introduces three new enum which allows you to instantiate TimeZone
, Calendar
and Locale
in a more safe way.
Calendar Shortcut: CalendarName
CalendarName
is an enum which define all possible values for Calendar
object.
The only property of CalendarName
is .calendar
: by calling it you will get the appropriate instance of Calendar
object.
Examples
TimeZone Shortcut: TimeZoneName
TimeZoneName
is an enum which define all pre-defined possible values for TimeZone
objects.
The only property of TimeZoneName
is .timezone
: by calling it you will get the appropriate instance of TimeZone
object.
Examples
Locale Shortcut: LocaleName
LocaleName
is an enum which define all pre-defined possible values for LocaleName
objects.
The only property of LocaleName
is .locale
: by calling it you will get the appropriate instance of Locale
object.
Examples
TimeInterval
TimeInterval
is a typealias used to measure the number of seconds.
SwiftDate introduces two extra functions for this type of object:
.in(components: [Calendar.Component], of: CalendarName)
function: it allows to express a time interval in one or more time components unit.string(options: ComponentsFormatterOptions)
function: it allows to express a time interval in a string of components units
Examples
DateComponents
DateComponents
in SwiftDate contains several extensions
First of all DateComponents
contains a property for each time unit; this allows SwiftDate to support math operators between Date/DateInRegion
and time components: let newDate = beforeDate + 2.days - 4.months + 3.hours
Moreover there are the following functions:
Create date by adding components from a Date or DateInRegion
.from(date: Date, in region: Region? = nil)
function: Create a newDate
in absolute time from a specific date by adding self components.from(dateInRegion: DateInRegion) -> DateInRegion?
function: Create a newDateInRegion
from anotherDateInRegion
by adding self components
Create date by subtracting components from a Date or DateInRegion
.ago(from date: Date, in region: Region? = nil) -> Date?
function: Create a newDateInRegion
from anotherDateInRegion
by subtracting self components.ago(fromDateInRegion date: DateInRegion) -> DateInRegion?
function: Create a newDateInRegion
from anotherDateInRegion
by subtracting self components
Create date by adding/subtracting components from current datetime (both for Date and DateInRegion)
.fromNow(in region: Region? = nil) -> Date?
function: Create a newDate
in absolute time from current date by adding self components.ago(in region: Region? = nil) -> Date?
function: Create a newDateInRegion
from current by subtracting self components