O'Reilly logo

Mastering Swift 4 - Fourth Edition by Jon Hoffman

Stay ahead with the world's most comprehensive technology and business learning platform.

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, tutorials, and more.

Start Free Trial

No credit card required

DateFormatter

The DateFormatter class is a subclass of the Formatter abstract class that can be used to convert a Date object into a human-readable string. It can also be used to convert a String representation of a date into a Date object. We will look at both use cases in this section. Let's begin by seeing how we could convert a Date object into a human-readable string.

The DateFormatter type has five predefined styles that we can use when we are converting a Date object to a human-readable string. The following chart shows what the five styles look like for an en-US locale:

DateFormatter Style

Date

Time

.none

No format

No format

.short

12/25/16

6:00 AM

.medium

Dec 25, 2016

6:00:00 AM

.long

December ...

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, interactive tutorials, and more.

Start Free Trial

No credit card required