Search code examples
swiftdatensdateepochiso8601

Swift: Convert ISO8601 to Unix Epoch


I'd like to take a ISO8601 timestamp, like this: YYYY-MM-DDTHH:MM:SSZ, and convert it to a Unix epoch time stamp using Swift.


Solution

  • ISO8601DateFormatter converts the string to Date, timeIntervalSince1970 gets the UNIX timestamp.

    let timestamp = ISO8601DateFormatter().date(from: "2017-07-14T20:00:00Z")!.timeIntervalSince1970