iOS Closures in Swift Building Standard Library Functions Map

Shaan Marok
Shaan Marok
Pro 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 string)

let dateStrings = ["20 Oct 1978", "11 Jan 1947", "28 March 2002"]
dateStrings.customMap { $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

Michael Psoinos
Michael Psoinos
8,132 Points

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.