ios - How to get NSDate from string without converting it to UTC -
i have following problem. let's suppose receive server string containing date time in following format:
yyyy-mm-dd't'hh:mm:ssxxxx
this server date time , display in application is. if receive date time:
2017-04-07t10:29:23+ 02:00
it means on server 10:29:23 (08:29:23 utc)
and display in application as:
04.07.2017 10:29:23
but whenever try convert string nsdate date converted utc not desired because lose information server time zone. use following code right now:
let formatter = nsdateformatter() formatter.calendar = nscalendar(calendaridentifier: nscalendaridentifieriso8601) formatter.locale = nslocale(localeidentifier: "en_us_posix") formatter.dateformat = "yyyy-mm-dd't'hh:mm:ssxxxx" let date = formatter.datefromstring(datestring)
how can achieve in swift (2.3)?
thanks in advance.
nsdate
not have built-in time zone. if print
nsdate
it'll print in gmt because has print in something. date not contain time zone, , can't. they're time-zone independent.
to print date in particular time zone, configure nsdateformatter
format , time zone want, use .stringfromdate
. or don't set time zone , it'll print in device's time zone , locale, being user expects (e.g. server says "event happened @ 00:23+02:00" cet user you'll display "event happened @ 23:23", , est user display "event happened @ 6:23pm").
frustratingly, have 1 problem left: date formatter understands time zone in original string (the +02:00), can't communicate means. if want take nsdate
, print in same time zone server you're going have determine time zone on own, communicate via channel, or hard-code it.
Comments
Post a Comment