-1

I'm using this code for converting array of String date into Date

let formatter = DateFormatter()
formatter.dateFormat = "yyyy-MM-dd'T'HH:mm:ss"
for i in 0 ..< data.count {
     let time = data[i]
     let ft_date = time["ft_date"] as! String
     print(ft_date)
     let dateF = formatter.date(from: ft_date)
     print(dateF)
}

and it's the output of console

2017-04-09T00:00:00
Optional(2017-04-08 19:30:00 +0000)
2017-04-08T00:00:00
Optional(2017-04-07 19:30:00 +0000)
2017-04-05T00:00:00
Optional(2017-04-04 19:30:00 +0000)
2017-04-01T00:00:00
Optional(2017-03-31 19:30:00 +0000)
2017-04-01T00:00:00
Optional(2017-03-31 19:30:00 +0000)
2017-04-01T00:00:00
Optional(2017-03-31 19:30:00 +0000)
2017-03-22T00:00:00
nil

why this is happening? it's the same format but I'm getting nil

Amir_P
  • 8,322
  • 5
  • 43
  • 92

2 Answers2

3

Are you living in a country where daylight saving time changes on 2017-03-22 at midnight?

If yes this could be the reason because the date does not exist.

vadian
  • 274,689
  • 30
  • 353
  • 361
  • I'm living in Iran and it's not changing on `2017-03-22` but how can I fix it? – Amir_P May 09 '17 at 06:38
  • According to [timeanddate.com](https://www.timeanddate.com/time/change/iran) it does change on `2017-03-22` in Iran – vadian May 09 '17 at 06:41
  • oh you're right. what should I do? – Amir_P May 09 '17 at 06:41
  • Daylight starts on 22 March 2017 in Iran as linked [here](https://www.timeanddate.com/time/change/iran/tehran). [This](http://stackoverflow.com/a/34533387/3581421) answer may help you. – ridvankucuk May 09 '17 at 06:42
  • my date is String so I can't use `isDaylightSavingTimeForDate` because it gets a `NSDate` – Amir_P May 09 '17 at 06:51
0

You need to change time Format ;

formatter.dateFormat = "yyyy-MM-dd HH:mm:ss Z"

enter image description here

KKRocks
  • 8,222
  • 1
  • 18
  • 84