Contents

parse(_:)

Parses a string into a date.

Declaration

func parse(_ value: String) throws -> Date

Parameters

  • value:

    The string to parse.

Return Value

An instance of Date parsed from the input string.

Discussion

This method attempts to parse a provided string into an instance of date using the source date format style. The function throws an error if it can’t parse the input string into a date instance.

The date format style guides parsing the date instance from an input string, as the following example illustrates.

let birthdayFormatStyle = Date.ISO8601FormatStyle()    
    .dateSeparator(.dash)
    .timeSeparator(.colon)
    .year()
    .month()
    .day()
    .time(includingFractionalSeconds: false)

// Create a date instance from a string representation of a date.
let yourBirthdayString = "2021-02-17T14:33:25"
let yourBirthday = try? birthdayFormatStyle.parse(yourBirthdayString)
// Feb 17, 2021 at 8:33 AM

See Also

Parsing an ISO 8601 Format Style