Convert Date with +1000 values

对着背影说爱祢 提交于 2021-01-29 08:54:44

问题


I have a date like this : 2018-04-30T23:22:05+1000 and want to convert in to Date Object

let formatter = DateFormatter()
formatter.dateFormat = "yyyy-MM-dd'T'HH:mm:ss:Z" // z, zz also still nil
let ss = formatter.date(from: "2018-04-30T23:22:05+1000")
print("VAL : \(ss)") // nil always

What is the correct format for +1000 values?

Also what locale i have to add to get exact date?


回答1:


use your dateformat as zone - The long specific non-location format. Where that is unavailable, falls back to the long localized GMT format (“0000”).

                             //2018-04-30T23:22:05+1000
       formatter.dateFormat = "yyyy-MM-dd'T'HH:mm:ssZ" 

instead of

       formatter.dateFormat = "yyyy-MM-dd'T'HH:mm:ss:Z" // z, zz also still nil

full answer

    let formatter = DateFormatter()
    formatter.dateFormat = "yyyy-MM-dd'T'HH:mm:ssZ" 
    let ss = formatter.date(from: "2018-04-30T23:22:05+1000")
    print("VAL : \(ss)") // nil always




回答2:


The date is an ISO 8601 date (Internet format) and the +1000 means +10 hours and 0 minutes (+hhmm). Parse it like this

let str = "2018-04-30T23:22:05+1000"
let formatter = ISO8601DateFormatter()

formatter.formatOptions = .withInternetDateTime

let date = formatter.date(from: str)


来源:https://stackoverflow.com/questions/50809718/convert-date-with-1000-values

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