DateFormater error only when running on iOS 13

为君一笑 提交于 2020-01-24 20:59:31

问题


I have a function to convert string to date format. This function works as expected on iOS 12 but on iOS 13. I get this error:

"Thread 1: Fatal error: Unexpectedly found nil while unwrapping an Optional value"

Here is my code:

func ConvertDateAndTimeFormat2() {
     let timeDate = "2019-09-24 15:00:00 +0000"
     let dateFormatter = DateFormatter()
     dateFormatter.dateFormat = "yyyy-MM-dd HH:mm:ss +zzzz"
     dateFormatter.timeZone = TimeZone(abbreviation: "GMT+3:00")
     var dateObj:Date!
     dateObj = dateFormatter.date(from: timeDate)
     dateFormatter.dateFormat = "EEEE, MMM d, yyyy"
     dateFormatter.timeZone = TimeZone(abbreviation: "GMT+3:00")

     let timeFormatter = DateFormatter()

     timeFormatter.dateFormat = "yyyy-MM-dd HH:mm:ss +zzzz"
     timeFormatter.timeZone = TimeZone(abbreviation: "GMT+3:00")
     var timeObj:Date!
     timeObj = timeFormatter.date(from: timeDate)

     timeFormatter.dateFormat = "HH:mm"
     timeFormatter.timeZone = TimeZone(abbreviation: "GMT+3:00")
     let timef = "\(timeFormatter.string(from: timeObj!))"
     let Date = "\(dateFormatter.string(from: dateObj!))"
 }

回答1:


Please change your dateformat and use "yyyy-MM-dd HH:mm:ss Z" and try.

let dateFormatter = DateFormatter()
dateFormatter.dateFormat = "yyyy-MM-dd HH:mm:ss Z"

For more details please visit https://nsdateformatter.com/




回答2:


There are many issues with your code. There is no reason to create two Date objects from one string. Just parse the original string once. Then you can create your date string and time string with the desired formats from that one date.

You should also use the special locale of en_US_POSIX when parsing fixed format date strings. There is also no need to set a timezone when parsing the original date string. The string provides its own timezone. The +0000 means it is UTC time.

You may not want to provide a timezone when converting the date to your final strings either. Most likely you want strings in the user's locale timezone, not some hardcoded timezone.

Here's your code cleaned up a lot:

func convertDateAndTimeFormat2() {
    let timeDate = "2019-09-24 15:00:00 +0000"

    let dateFormatter = DateFormatter()
    dateFormatter.dateFormat = "yyyy-MM-dd HH:mm:ss Z"
    dateFormatter.locale = Locale(identifier: "en_US_POSIX")
    if let dateObj = dateFormatter.date(from: timeDate) {
        dateFormatter.dateFormat = "EEEE, MMM d, yyyy"
        dateFormatter.timeZone = TimeZone(abbreviation: "GMT+3:00") // Probably not needed

        let timeFormatter = DateFormatter()
        timeFormatter.dateFormat = "HH:mm"
        timeFormatter.timeZone = TimeZone(abbreviation: "GMT+3:00") // Probably not needed

        let timef = timeFormatter.string(from: dateObj)
        let datef = dateFormatter.string(from: dateObj)
        print("Date: \(datef), time: \(timef)")
    }
}

convertDateAndTimeFormat2()

Output:

Date: Tuesday, Sep 24, 2019, time: 18:00




回答3:


If you are trying to convert string time which in 24 hours format (Ex 15:25:00 / HH:mm:ss) then enable your device 24-Hour Time option. It might solve the issue in IOS 13.3

You may find the option here

Settings -> General -> Date & Time



来源:https://stackoverflow.com/questions/58092714/dateformater-error-only-when-running-on-ios-13

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!