Optional Date is not right working on my code. This is my code.
extension String {
func toDate(format: String = "yyyyMMddHHmmssSSS") -> Date? {
let dateFormatter = DateFormatter()
dateFormatter.locale = Locale(identifier: "en_US_POSIX")
dateFormatter.timeZone = TimeZone(identifier: "Asia/Tokyo")
dateFormatter.dateFormat = format
return dateFormatter.date(from: self)
}
}
apiOutput has these datas.
▿ apiOutput
...
▿ stockStartDate : Optional<String>
- some : "20200722"
▿ stockEndDate : Optional<String>
- some : "20200722"
...
I executed this code.
let stockStartDate = apiOutput.tmStockStartDate?.toDate(format: "yyyyMMdd")
Then, I want have a result is stockStartDate has optional-date value, but this result is nil.
addition, i tried the variant print on LLDB-console, then the variant has a result i want.
(lldb) po stockStartDate
▿ Optional<Date>
▿ some : 2020-07-21 15:00:00 +0000
- timeIntervalSinceReferenceDate : 617036400.0
I can't understand what's happend.
P.S.
I'm sorry. My English is so bad. Please let me know if I say something that's unclear.