In api call response I'm getting dates in different timezone. I want to convert it to user's local timezone plus user can select any timezone from the list of ios timezone. This is all local. We would never be sending selected user timezone to server.
And while making any api call lets say i am creating an event with selected start date in a picker. I should send it global timezone not with the user selected timezone because it is local to the app.
Currently I am changing defaultTimeZone on selection so that I get proper current date etc. And using NSDateFormatter with setTimeZone ... [NSTimeZone timeZoneWithName...
Is this a better approach to change defaultTimeZone which is all local to the app ? How should I convert the NSdate back to the global date while sending it to the server ?
NSDate
is a point in time, you might call it absolute time. This time is the same for every place on earth.NSDate
does not represent a time that is formatted to the likings of the people at a specific location. This differentiation is very important.A single NSDate() can have multiple representations in multiple timezones.
To make it more visual, these 5 clocks all represent a single point in time (the
NSDate
instance):They just show a different representation of the same point in time.
One could say these clocks are what
NSDateFormatter.stringFromDate()
is. They turn the absolute point in time into a local representation.To convert between different timezones you don't change
NSDate
, you change the timezone ofNSDateFormatter
. If the date string you get from your server is in UTC, you need a NSDateFormatter which uses UTC as timeZone:To display that server time to your user, you take that NSDate and put it into another dateFormatter:
And if you want to send a date string back to your server, you take a NSDate and put it through your UTC dateFormatter.