DateFormatter Date and Time Cheatsheet
A cheatsheet and a manager?
There is little that annoys me as much as the way that dates require a dateformatter that just hangs around in the parent class. There must be a better way!
Also the formats: dd
DD
ddd
mean, what exactly? Perhaps there is a way to actually let Swift help us out and provide readable code?
Indeed, and this article is it.
Difficulty: Beginner | Easy | Normal | Challenging
Prerequisites:
- Be able to code Swift using Playgrounds
Terminology
Date
: A specific point in time, independent of any calendar or time zone
DateFormatter
: A formatter that converts between dates and their textual representations
DataComponents
: A date or time specified in terms of units (such as year, month, day, hour, and minute) to be evaluated in a calendar system and time zone
Motivation
Much like the difference between NSData
and Data
, the difference between them being…just use the non NS version OK (or look at that link).
Due to the different versions of Date
, there can be some confusion in using DateFormatter
and, well — let’s get…