简体   繁体   中英

Returning nil when converting string to date in swift

My date format in string is "2019-03-28".

My date formatter is like "yyyy-MM-dd"

let dateFormatter = DateFormatter()
dateFormatter.locale = Locale(identifier: "en_US_POSIX")
dateFormatter.dateFormat = "yyyy-MM-dd"
dateFormatter.timeZone = TimeZone(abbreviation: "GMT+0:00")
let assignDate = dateFormatter.date(from: deliveryDate)

What am I doing wrong here

let dateFormatter = DateFormatter()
dateFormatter.locale = Locale(identifier: "en_US_POSIX")
dateFormatter.dateFormat = "yyyy-MM-dd"
dateFormatter.timeZone = TimeZone(abbreviation: "GMT+0:00")
let assignDate = dateFormatter.date(from: "2019-03-28")
print("assignDate \(assignDate)")

All works fine for me here!

assignDate Optional(2019-03-28 00:00:00 +0000)

let dateFormatter = DateFormatter()
dateFormatter.locale = Locale(identifier: "en_US_POSIX")
dateFormatter.dateFormat = "yyyy-MM-dd"
dateFormatter.timeZone = TimeZone(abbreviation: "GMT+0:00")
let assignDate = dateFormatter.date(from: deliveryDate)

This is working fine

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM