1

I am trying to build a bridge between flutter and swift in iOS for a little widget. The problem I am facing is converting the DateTime.now().toIso8601String into a usable Date Object in Swift, this is how my code looks like:

        let shared = sharedDefaults?.object(forKey: "widgetData") as? [String: String] ?? [String: String]()
        
            let isoDateFormatter = ISO8601DateFormatter()
            isoDateFormatter.timeZone = TimeZone(abbreviation: "GMT")
            isoDateFormatter.formatOptions = [.withInternetDateTime,
                                              .withDashSeparatorInDate,
                                              .withFullDate,
                                              .withFractionalSeconds,
                                              .withColonSeparatorInTime]
            let realDate = isoDateFormatter.date(from: shared["start"]!)
            print(realDate)
            print(shared["start"]!)
          flutterData = FlutterData(title: shared["title"]!,
                                        comment: shared["comment"]!,
                                        end: nil,
                                        start: realDate ?? Date())

I have built in a fallback to the current Date() but I don't seem to manage to convert the DateTime String to a swift Date Object

here is how it looks like when printed:

2021-09-18T14:56:50.606258

Joakim Danielson
  • 43,251
  • 5
  • 22
  • 52
Moel
  • 39
  • 7
  • I think it’s the number of fractional digits that’s the problem, try a normal DateFormatter with the format "yyyy-MM-dd'T'HH:mm:ss.SSSSSS" – Joakim Danielson Sep 18 '21 at 14:45

2 Answers2

0

It seems to work with the following options

Be cautious about withFractionalSeconds option

Prior to macOS 10.13 / iOS 11 ISO8601DateFormatter does not support date strings including milliseconds.

let isoDateFormatter = ISO8601DateFormatter()
isoDateFormatter.timeZone = TimeZone(abbreviation: "GMT")
isoDateFormatter.formatOptions = [.withDashSeparatorInDate,
                                  .withFullDate,
                                  .withTime,
                                  .withFractionalSeconds,
                                  .withColonSeparatorInTime]

let dateString = "2021-09-18T14:56:50.606258"
let realDate = isoDateFormatter.date(from: dateString)
print(realDate) // prints w/o fractional seconds
let calendar = Calendar.autoupdatingCurrent
let comps = calendar.dateComponents([.nanosecond], from: realDate!)
print(comps) // print nanoseconds part
schmidt9
  • 4,436
  • 1
  • 26
  • 32
0

thanks to Joachim Danielson for his comment, this worked for me:

        let dateFormatter = DateFormatter()
        dateFormatter.dateFormat = "yyyy-MM-dd'T'HH:mm:ss.SSSSSS"
        let realDate = dateFormatter.date(from: shared["start"]!)
Moel
  • 39
  • 7
  • Your code as it is might fail it the user device's 24-Hour setting is OFF. Note also that it the date string timezone is UTC it will parse the date using the current timezone. So you should set the dateFormatter locale to "en_US_POSIX" BEFORE setting the dateFormat. Don't forget also to set the timezone in case your date string is UTC – Leo Dabus Sep 18 '21 at 15:29