无法从格式“ yyyy-MM-dd'T'HH:mm:ss + 00:00”创建日期

时间:2018-07-01 22:25:15

标签: swift dateformatter

我想从字符串转换为日期。 我在这里使用“ yyyy-MM-dd'T'HH:mm:ss + 00:00” 正确的格式吗?

extension String {
    func format(_ format:String) -> String {
        var date: Date?
        let dateFormatterGet = DateFormatter()
        let dateFormatterOutput = DateFormatter()
        dateFormatterGet.dateFormat = "yyyy-MM-dd'T'HH:mm:ss+00:00"
        dateFormatterOutput.dateFormat = format
        if let d = dateFormatterGet.date(from: self) {
            date = d
        }
        return dateFormatterOutput.string(from: date!)
    }
}

extension Date {
    func string(with format: String) -> String {
        let dateFormatter = DateFormatter()
        dateFormatter.dateFormat = format
        return dateFormatter.string(from: self)
    }
}

1 个答案:

答案 0 :(得分:0)

问题所在:

ERROR 1064 (42000) at line 36: You have an error in your SQL syntax; check the manual that corresponds to your MariaDB server version for the right syntax to use near 'procedure upsert_person (
in pId int unsigned,
in pName varchar(255),
' at line 1

dateFormatterGet.dateFormat = "yyyy-MM-dd'T'HH:mm:ss+00:00" 需要替换为+00:00,例如:

xxxx

您的代码将变为:

dateFormatterGet.dateFormat = "yyyy-MM-dd'T'HH:mm:ssxxxx"

注意:在这里,我假设您使用的日期字符串包含的时区为 + xx:xx(例如+02:00),依此类推上。但是,对于其他格式,您需要使用不同数量的func format(_ format:String) -> String { var date: Date? let dateFormatterGet = DateFormatter() let dateFormatterOutput = DateFormatter() dateFormatterGet.dateFormat = "yyyy-MM-dd'T'HH:mm:ssxxxx" dateFormatterOutput.dateFormat = format if let d = dateFormatterGet.date(from: self) { date = d } return dateFormatterOutput.string(from: date!) } (如果时区不包含Z,则使用较小的x

  1. x-> -08,+0330
  2. x-> -0800
  3. xx-> -800,-075254
  4. xxx-> -8:00,-07:52:54

如果时区包含xxxx,则我们使用大写字母Z