ios - Convert this date string to NSDate "2016-09-23T23:06:00+00:00" -


i trying convert string nsdate, works usa location, when changed brazil. bumm , crashes! format string should use?

this code:

static func texttodatedatestring: string) -> nsdate {     let dateformatter = nsdateformatter()     dateformatter.dateformat = "yyyy-mm-dd't'hh:mm:ss+00:00"     let date = dateformatter.datefromstring(datestring)!      return date } 

you ignoring timezone info @ string. way inputing time local time when utc +00:00. forcing unwrapping string, crash app if input invalid string date. try this:

func texttodate(datestring: string) -> nsdate? {     let dateformatter = nsdateformatter()     dateformatter.calendar = nscalendar(calendaridentifier: nscalendaridentifieriso8601)!     dateformatter.dateformat = "yyyy-mm-dd't'hh:mm:ssxxx"     dateformatter.locale = nslocale(localeidentifier: "en_us_posix")     return dateformatter.datefromstring(datestring) }  if let datefromstring = texttodate("2016-09-23t23:06:00+00:00") {     print(datefromstring)   // "2016-09-23 23:06:00 +0000" } 

Comments