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.
Swift Date Time Formatting Overview 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. Updated: 22nd October 2015. String to NSDate. Var dateString = ' var dateFormatter = NSDateFormatter // this is imporant - we set our input date format to match our input string dateFormatter.dateFormat = 'dd-MM-yyyy' // voila! Var dateFromString = dateFormatter.dateFromString(dateString).
For more information, see Technical Q&A QA1480 “NSDateFormatter and Internet Dates”. On iOS 7 and later NSDate Formatter is thread safe. In macOS 10.9 and later NSDate Formatter is thread safe so long as you are using the modern behavior in a 64-bit app. DateFormatter is an InternationalFormatter that does its formatting by way of an instance of java.text.DateFormat. Warning: Serialized objects of this class will not be compatible with future Swing releases. The current serialization support is appropriate for short term storage or RMI between applications running the same version of Swing.
Characters | Example | Description |
---|---|---|
Year | ||
y | 2018 | Year, no padding |
yy | 18 | Year, two digits (padding with a zero if necessary) |
yyyy | 2018 | Year, minimum of four digits (padding with zeros if necessary) |
Quarter | ||
Q | 2 | The quarter of the year. Use QQ if you want zero padding. |
QQQ | Q2 | Quarter including “Q” |
QQQQ | 2nd quarter | Quarter spelled out |
Month | ||
M | 11 | The numeric month of the year. A single M will use ‘1’ for January. |
MM | 11 | The numeric month of the year. A double M will use ’01’ for January. |
MMM | Nov | The shorthand name of the month |
MMMM | November | Full name of the month |
MMMMM | N | Narrow name of the month |
Day | ||
d | 26 | The day of the month. A single d will use 1 for January 1st. |
dd | 26 | The day of the month. A double d will use 01 for January 1st. |
F | 4th Wednesday in December | The day of week in the month |
E | Weds | The day of week in the month |
EEEE | Wednesday | The full name of the day |
EEEEE | W | The narrow day of week |
Hour | ||
h | 5 | The 12-hour hour. |
hh | 05 | The 12-hour hour padding with a zero if there is only 1 digit |
H | 17 | The 24-hour hour. |
HH | 17 | The 24-hour hour padding with a zero if there is only 1 digit. |
a | PM | AM / PM for 12-hour time formats |
Minute | ||
m | 25 | The minute, with no padding for zeroes. |
mm | 25 | The minute with zero padding. |
Second | ||
s | 1 | The seconds, with no padding for zeroes. |
ss | 01 | The seconds with zero padding. |
Time Zone | ||
zzz | EST | The 3 letter name of the time zone. Falls back to GMT-08:00 (hour offset) if the name is not known. |
zzzz | Eastern Standard Time | The expanded time zone name, falls back to GMT-08:00 (hour offset) if name is not known. |
zzzz | CST-04:00 | Time zone with abbreviation and offset |
Z | -0400 | RFC 822 GMT format. Can also match a literal Z for Zulu (UTC) time. |
ZZZZZ | -04:00 | ISO 8601 time zone format |
iOS and macOS contain built in support for various ways to format dates, such as for human readable text and to show intervals between dates.
At WWDC19, Apple added a new RelativeDateTimeFormatter, which formats relative dates from the current date, for example by formatting a past date as “X days ago” or today as “today”.
This guide explores how to display and format dates using many of the different date formatters built into Apple’s platforms.
As all of the built in date formatters are available in Apple’s Foundation framework, no installation is required to use them. However, make sure to add import Foundation to the top of your file to be able to use them in your code.
If you’d like to follow along with this guide, just create a Swift Playground using Xcode or the Swift Playgrounds app for iPad.
Additionally, you can click any of the class or header names to go to Apple’s official Developer Documentation.
Formatting basic dates is easy with DateFormatter. It supports many different time and date styles, allowing you to get the format that you want. Using android file transfer on mac.
Just create a formatter and set its time and date style to use it: Mitch for twitch 1 6 3.
DateFormatter can be used with either a date or time style, or both.
The .none Topaz jpeg to raw ai 2 2 0 3. style ignores the date and only uses the time style.
The .short style only uses numbers without text.
The .medium style is more human readable and uses the short name of the month.
The .long style is the same as the medium style but slightly longer, using the full name of the month.
The .full style displays the entire date, including the name of the week.
The .none style ignores the time and only uses the date style.
The .short style is the simplest time style and just shows the hours, minutes, and whether it is currently AM or PM.
The .medium style is the same as the short style but also displays seconds.
The .long style is the same as the short style but also displays the time zone.
The .full style displays everything from the other time styles as well as the entire name of the time zone.
DateComponentsFormatter formats DateComponents instances, which contain dates and times specified in terms of units.
Just create a formatter and pass an instance of DateComponents, and the formatter will represent the components the best it can.
DateComponentsFormatter contains five unit styles, which format the date components in different ways.
The .positional style is the default style, and uses typical date position formatting instead of separating each component like the abbreviated style.
The .abbreviated style is the shortest style apart from the positional style and abbreviates the components as much as possible.
The .short style uses slightly longer versions of each date component.
The .full style uses the full name for each date component.
The .spellOut style is the longest style and uses spelled out versions of everything.
Clark lift model c500 45 manual.
DateIntervalFormatter is similar to the basic DateFormatter, but it displays both a beginning and end date, such as 21-26 July 2019.
To use it, create a formatter and generate a string from two dates:
Note that by default, the output will be based on the locale and time style from the device preferences, so you might see something different.
DateIntervalFormatter uses the same date styles that the basic date formatter does. Here are examples of each style:
DateIntervalFormatter uses the same time styles that the basic date formatter does. Here are examples of each style:
Note:RelativeDateTimeFormatter requires Xcode 11 and the latest beta versions of macOS 10.15 or iOS 13, which are currently in beta, as it is not available on previous versions.
RelativeDateTimeFormatter is a new date formatter from WWDC19 that formats relative dates as the amount of time between two dates or according to the current date and time.
Omnifocus pro 2 2 9 1. To use it, create a formatter and generate a string from two dates.
RelativeDateTimeFormatter supports different date/time and unit styles, which we’ll go through later on.
You can also use a instance of DateComponents, which will format it based on the current date.
As a bonus, all the strings that are generated are already localized, so you don’t need to worry about localization.
RelativeDateTimeFormatter supports two different date/time styles, which can be used by setting them before generating the string.
The .numeric style is the default style and always uses the literal definition of the date.
The .named style falls back to the numeric style, but when possible, uses relative names such as yesterday or tomorrow.
RelativeDateTimeFormatter contains four different unit styles, which format the date in different ways.
The .abbreviated style is the shortest style and abbreviates as much as possible.
The .short style is identical to the abbreviated style in English, but might generate different strings in other languages.
The .full style is the default style and uses longer names, such as month instead of mo..
The .spellOut style is the longest style and uses spelled out versions of everything.
In this guide, you learned about the different date formatters that are built into Apple’s platforms, and how to use them.
I hope this post was useful and taught you something new.
Thanks for reading