Swift Date Formats

Swift Date Time Formatting

Consider formatters. Both Swift and Cocoa/Cocoa touch support a number of these, ranging from numbers and currency to dates and times. They are a natural interpolation fit. For example, dates are easy right out of the box. Just pass a formatter and you’re done. Note that I skip the formatter label. You’ll see why in just a second. The JSON date format for a Date got the value of 57786402. This almost looks like a unix epoch timestamp, but if you put it into an online converter it’ll come out as @ 4:06pm. It’s not a unix timestamp. It’s actually using a different reference date – 2001 instead of 1970. Standard Approach, using a DateFormatter 🗓 As mentioned, the DateFormatter approach set's up the timezone, set's up the string format for the date, and then uses the formatter to provide the string from Foundation's date object! Looking at this a bit more in-depth, notice that the timeZone on the formatter is set to the.current timeZone. The Date struct is used to represent dates and times in Swift 5, and it’s designed to do so in the most flexible way possible: as a number of seconds relative to the start of the Third Millennium, January 1, 2001, 00:00:00 UTC.

Swift Date FormatsSwift date formats


Overview

Swift Date Format

Swift Date Formats

Swift is a programming language for macOS, iOS, watchOS and tvOS. Chances are if you are developing a mobile app for iphones, ipads or iwatches, you’ll need to learn swift. 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.

Patterns

Swift
CharactersExampleDescription
Year
y2018Year, no padding
yy18Year, two digits (padding with a zero if necessary)
yyyy2018Year, minimum of four digits (padding with zeros if necessary)
Quarter
Q2The quarter of the year. Use QQ if you want zero padding.
QQQQ2Quarter including “Q”
QQQQ2nd quarterQuarter spelled out
Month
M11The numeric month of the year. A single M will use ‘1’ for January.
MM11The numeric month of the year. A double M will use ’01’ for January.
MMMNovThe shorthand name of the month
MMMMNovemberFull name of the month
MMMMMNNarrow name of the month
Day
d26The day of the month. A single d will use 1 for January 1st.
dd26The day of the month. A double d will use 01 for January 1st.
F4th Wednesday in DecemberThe day of week in the month
EWedsThe day of week in the month
EEEEWednesdayThe full name of the day
EEEEEWThe narrow day of week
Hour
h5The 12-hour hour.
hh05The 12-hour hour padding with a zero if there is only 1 digit
H17The 24-hour hour.
HH17The 24-hour hour padding with a zero if there is only 1 digit.
aPMAM / PM for 12-hour time formats
Minute
m25The minute, with no padding for zeroes.
mm25The minute with zero padding.
Second
s1The seconds, with no padding for zeroes.
ss01The seconds with zero padding.
Time Zone
zzzESTThe 3 letter name of the time zone. Falls back to GMT-08:00 (hour offset) if the name is not known.
zzzzEastern Standard TimeThe expanded time zone name, falls back to GMT-08:00 (hour offset) if name is not known.
zzzzCST-04:00Time zone with abbreviation and offset
Z-0400RFC 822 GMT format. Can also match a literal Z for Zulu (UTC) time.
ZZZZZ-04:00ISO 8601 time zone format

Swift Date Time

Date Time Formatting in Swift 4

Swift Date Formatting

Date Time formatting in Swift 3