toDate 函数的 SwiftDate 替换 swift 3
SwiftDate replacement for toDate function swift 3
在 swift 2.x 我使用 toDate 函数使用 SwiftDate 获取 iso 日期。任何人都知道正确的替代品:
let isoDate = stringDate.toDate(DateFormat.ISO8601Format(.Extended))
查看 SwiftDate 文档,我试过这样的事情:
let isoDate = stringDate.date(format:.iso8601)
但它抱怨格式
编辑:
我将 SwiftDate 版本从 4.0.7 更改为 4.0.3,现在存在 iso 格式但抛出错误
我的日期字符串:"2016-11-24T03:00:00.000Z"
let isoDate = try! dt.date(format: DateFormat.iso8601(options: .extended)).absoluteDate
错误:类型 'ISO8601DateTimeFormatter.Options' 没有成员 'extended'
let isoDate = try! dt.date(format: DateFormat.iso8601(options: [])).absoluteDate
错误:没有错误但抛出
在swift 2.2中我使用了完全相同的日期格式。
SwiftDate 4.0.5
- #293 Added .withInternetDateTimeExtended as options of ISO8601DateTimeFormatter
这里是 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)
这是在当前最新版本 (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)
在 swift 2.x 我使用 toDate 函数使用 SwiftDate 获取 iso 日期。任何人都知道正确的替代品:
let isoDate = stringDate.toDate(DateFormat.ISO8601Format(.Extended))
查看 SwiftDate 文档,我试过这样的事情:
let isoDate = stringDate.date(format:.iso8601)
但它抱怨格式
编辑:
我将 SwiftDate 版本从 4.0.7 更改为 4.0.3,现在存在 iso 格式但抛出错误
我的日期字符串:"2016-11-24T03:00:00.000Z"
let isoDate = try! dt.date(format: DateFormat.iso8601(options: .extended)).absoluteDate
错误:类型 'ISO8601DateTimeFormatter.Options' 没有成员 'extended'
let isoDate = try! dt.date(format: DateFormat.iso8601(options: [])).absoluteDate
错误:没有错误但抛出
在swift 2.2中我使用了完全相同的日期格式。
SwiftDate 4.0.5
- #293 Added .withInternetDateTimeExtended as options of ISO8601DateTimeFormatter
这里是 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)
这是在当前最新版本 (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)