Swift Date Formats

A Date value encapsulate a single point in time, independent of any particular calendrical system or time zone. Date values represent a time interval relative to an absolute reference date. The Date structure provides methods for comparing dates, calculating the time interval between two dates, and creating a new date from a time interval relative to another date. Working with dates is a task that is universally applicable to Swift developers. Particularly when dealing with an API, dates can arrive in all shapes and sizes. We‘ll examine some of the common ones such as ISO 8601, show how to parse these formats into Date instances, and how to use DateFormatter to display them back again as a string.

Working with Dates in Swift

Working with the SWIFT community, SWIFT Standards operates the annual maintenance process for MT, which ensures that the standard evolves to meet changing market needs. SWIFT Standards, under contract to ISO, also maintains two open messaging standards: ISO 15022, which is used for securities settlement and asset servicing, and ISO 20022, which. SWIFT message types are the format or schema used to send messages to financial institutions on the SWIFT (Society for Worldwide Interbank Financial Telecommunication) network. The original message types were developed by SWIFT and a subset was retrospectively made into an ISO standard, ISO 15022.

The Foundation framework makes it painless to convert a string to a date, but there are a few pitfalls you need to watch out for. The class we use to convert a string to a date in Swift is DateFormatter (or NSDateFormatter if you are using Objective-C). I encourage you to follow along with me. Fire up Xcode and create a playground.

To start working with dates in iOS you must be familiar with at least these three objects: Date, DateFormatter and DateComponents.

Date struct

Date is a struct that belongs to the Foundation framework.

It represents a specific point in time independent of any calendar or time zone.

Date structure bridges to the NSDate class.

It provides methods for creating dates, comparing dates and calculating time intervals between dates.

It DOES NOT do anything related to date format or conversion between string and date.

DateFormatter class

Belongs to the Foundation framework

A formatter providing methods for converting from Date to String and from String to Date.

It also allows you to customize the representation of the date string using predefines styles or building your own format.

It also supports localization.

DateComponents struct

Belongs to the Foundation framework.

This is a Date or Time object represented in units such as year, month, day, minute or hour in a particular calendar.

It provides direct access to different parts of date or time.

Now lets write some code to demonstrate how to work with dates in Swift.

Creating Date in Swift using Date()

Create a date with current date and time

Create a date by adding time interval-in seconds- to current date

Create a date by adding time interval – in seconds – since reference date

Swiftui format date

please note that reference date is 00:00:00 UTC on 1 January 2001.

Converting Date to String using DateFormatter

Result:

First we create an instance of DateFormatter


Next we choose a style for the date from 5 predefines styles (none, full, long, short, medium), select none to hide the date part.

Then choose a style for the time from 5 predefined styles (none, full, long, short, medium), select none to hide the time part.

Finally we use DateFormatter to get string representation of our date

If you do not want to use one of the predefines styles you can create your own using dateFormat property

MMMM: full month name

dd: day

yyyy:year

HH: hours in 24 format

mm: minutes

You can get a complete list of date format patterns here

Swift Utc Date

Converting String to Date using DateFormatter

The dateFormat property of the DateFormatter MUST match the date format of the string.

Comparing Dates

Date() conforms to Comparable protocol so you can simply use < , > and to compare two dates.

Create DateComponents from Date

First of all you need to set your calendar then use it to create components

Result

Date

Create Date form DateComponents

Result

Finally those was just few examples of what you can do with Date, DateFormatter and DateComponents.

Please tell me if you have any comments, questions or suggestions.

Swift Date Formatting

You might like Working with CALayer tutorial