问题
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