Welcome to the Treehouse Community

Want to collaborate on code errors? Have bugs you need feedback on? Looking for an extra set of eyes on your latest project? Get support with fellow developers, designers, and programmers of all backgrounds and skill levels here with the Treehouse Community!

Looking to learn something new?

Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and join thousands of Treehouse students and alumni in the community today.

Start your free trial

iOS Closures in Swift Building Standard Library Functions Map

Shaan Marok
Shaan Marok
Courses Plus Student 1,015 Points

DateFormatter date method

How does the DateFormatter determine the time?

struct Formatter {
    static let formatter = DateFormatter()

    static func date(from string: String) -> Date? {
        formatter.dateFormat = "d MMM yyy"
        return formatter.date(from: string)

let dateStrings = ["20 Oct 1978", "11 Jan 1947", "28 March 2002"]
dateStrings.customMap { Formatter.date(from: $0) }

prints [Optional(1978-10-20 04:00:00 +0000), Optional(1947-01-11 05:00:00 +0000), Optional(2002-03-28 05:00:00 +0000)]

1 Answer

A Date value encapsulate a single point in time, independent of any particular calendrical system or time zone. If you create a date from a year, month, and day, the stored value will represent the beginning of day (midnight) in UTC ("Coordinated Universal Time"). UTC represents 0 degrees longitude (the prime meridian which runs through Greenwich, U.K.). Time zones are then represented as offsets from this longitude. When the compiler displays the date and time to you, it automatically adjusts the date and time displayed to the time zone you are in. It even takes Daylight Savings into account. That's why the first one says 4:00 and the other two say 5:00.