let isoFormatter = ISO8601DateFormatter()

// Example 1: Using ISO8601 date formatter with date string that ends with whole seconds
let dateStringSeconds = "2021-10-06T11:27:55Z"
let newDateSec = isoFormatter.date(from: dateStringSeconds)
print(String(describing: newDateSec))        // 2021-10-06 11:27:55 +0000

// Example 2: Using ISO8601 date formatter with date string that ends with milliseconds
let dateStringMilliSeconds = "2021-10-06T11:27:55.341Z"
isoFormatter.formatOptions = [.withInternetDateTime, .withFractionalSeconds]
let newDateMilli = isoFormatter.date(from: dateStringMilliSeconds)
print(String(describing: newDateMilli))      // 2021-10-06 11:27:55 +0000