Swift Date Formats

Date Time Formatting in Swift is based off of the DateFormatter class which can be used to manipulate dates and times. An Instance of DateFormatter creates a string representation of NSDate objects, and can also convert textual representations of dates and times into NSDate objects. There is a date in the format of YYYY/MM/dd, so for example 2019/11/30. That date could be in one of seperate places of a data structure. The end result is a date which is extracted from two seperate places and is converted into a Swift date format class that can be used for other things.

Working with Dates in Swift

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.

Swift Date Formats

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

Swift parse date from string

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

Create a date by adding time interval – in seconds – since reference 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

Converting String to Date using DateFormatter

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

Comparing Dates

Swift Date Formatter

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

Swift Date From String

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 Formatter

You might like Working with CALayer tutorial