Create and Parse Dates

Overview

There are several ways to create a new date which is expressed in a particular geographic region; it can be created from a pair of Date and Region objects, parsed from a String which define a custom format (SwiftDate supports custom datetime formats, ISO8601 specs, RSS or AltRSS and .NET dates) or create by passing a set of Calendar.Component objects.

Obviously you can also generate new date derived from math operations between two other dates.

In this page are listed all methods related to date creations and are valid both for DateInRegion and Date.

Parse Date from String

Parse strings and transform them into date is a boring activity, you know. SwiftDate can help you by supporting a wide range of formats.

You can specify the format you want to use in format parameters: it can be a custom format string or a well known ISO8601 datetime, or again a .NET datetime string.
Declaration
func init(string: String, format: DateFormat, fromRegion region: Region? = nil) throws
Parameters
  • string: this is the string you want to parse
  • format: this is the format in which you expect the string is formatted. DateFormat is an enum: .custom (for custom format), iso8601 to parse all available specs for ISO8601 DateTime Format, extended to parse Extended DateTime format, .rss to parse both RSS and AltRSS datetime formats and .dotNET to parse .NET datetime strings.
  • region: is the region in which you want to express specified date. If nil or not specified Region.Local(( is used instead.
Result

Resulting object is a DateInRegion instance result of the parsing and expressed in given region.

Example

Create from now

A pratical shortcut to create a new DateInRegion which is expressed in current device's Region is to use the default init() function of the object itself.

Declaration
func init()
Result

Resulting object is DateInRegion which define the current moment (Date()) expressed in Region.Local(), a particular region defined as local device's .timeZone,.locale and .calendar (all settings are auto-updating)

Example

Create from Date & Region

You can create a new DateInRegion object which define a Date and express it in a particular geographic region. Once created all operations made using the new DateInRegion object are expressed in the geographic region; properties and functions automatically take care of all geographic and locale settings.

Declaration
func init(absoluteDate date: Date, in region: Region)
Parameters
  • date: define the absolute Date you want to use to create the new DateInRegion. Passed date is indipendent from any geographic timezone/calendar or locale because it's expressed in absolute time.
  • region: define the destination Region in which you want to express passed date
Result

Resulting object is a DateInRegion instance which represent passed date expressed in a specific region.

Example

Create from DateComponents

Another way to create a new DateInRegion is to pass a valid set of calendar's components encapsulated inside a DateComponents object. Both TimeZone, Locale and Calendar must be specified in DateComponents instance in order to get a valid result; if omitted a .MissingCalTzOrLoc exception will thrown.

If from given components a valid Date cannot be created a .FailedToParse exception will thrown.

Declaration
func init(components: DateComponents)
Parameters
  • components: components used to generate the new date
Result

Resulting object is a DateInRegion instance where the date is expressed by passed DateComponents objct.

Example

Create from Time Components

Is possible to create a new DateInRegion object by passing a set of Calendar.Component and relative values (defined in a Dictionary of type [Calendar.Component:Int] and specify a destination Region.

Declaration
func init(components: [Calendar.Component:Int], in region: Region)
Parameters
  • components: components used to generate the new date. It's a dictionary where keys are Calendar.Component and values are Int with relative value. Supported components are: .day, .era, .hour, .minute, .month, .nanosecond, .quarter, .second, .weekOfMonth, .weekOfYear, .weekday, .weekdayOrdinal, .year, .yearForWeekOfYear
  • region: is the region in which you want to express specified date. If nil or not specified Region.Local(( is used instead.
Result

Resulting object is a DateInRegion instance where the date is expressed by passed DateComponents objct.

Example

Copyright © 2016 Daniele Margutti - All right reserved