Search code examples
dateswift3xcode8isodate

SwiftDate replacement for toDate function swift 3


in swift 2.x I used toDate function to get iso date using SwiftDate. Any one knows the right replacement for this:

let isoDate = stringDate.toDate(DateFormat.ISO8601Format(.Extended))

Looking at the SwiftDate documentations, I've tried something like this:

let isoDate = stringDate.date(format:.iso8601)

but it complaints about format

EDIT:

I changed SwiftDate version from 4.0.7 to 4.0.3 and now iso format exists but throws an error

my date string: "2016-11-24T03:00:00.000Z"

let isoDate = try! dt.date(format: DateFormat.iso8601(options: .extended)).absoluteDate

error: Type 'ISO8601DateTimeFormatter.Options' has no member 'extended'

let isoDate = try! dt.date(format: DateFormat.iso8601(options: [])).absoluteDate

error: no error but throws

in swift 2.2 I used exactly the same date format.


Solution

  • According to the SwiftDate CHANGELOG:

    SwiftDate 4.0.5

    • #293 Added .withInternetDateTimeExtended as options of ISO8601DateTimeFormatter

    And here is the source file:

    // The format used for internet date times; it's similar to .withInternetDateTime
    // but include milliseconds ('yyyy-MM-dd'T'HH:mm:ss.SSSZZZZZ').
    public static let withInternetDateTimeExtended = ISO8601DateTimeFormatter.Options(rawValue: 1 << 11)
    

    This compiles on the current latest version (SwiftDate 4.0.7):

    let stringDate = "2016-11-24T03:00:00.000Z"
    let options = ISO8601DateTimeFormatter.Options.withInternetDateTimeExtended
    let format = DateFormat.iso8601(options: options)
    let isoDate = try? stringDate.date(format: format)