How to convert server response date to local timezone?

冷暖自知 提交于 2019-12-30 07:32:32

问题


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 ?


回答1:


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 of NSDateFormatter. If the date string you get from your server is in UTC, you need a NSDateFormatter which uses UTC as timeZone:

let utcDateFormatter = NSDateFormatter()
utcDateFormatter.timeZone = NSTimeZone(forSecondsFromGMT: 0)
utcDateFormatter.locale = NSLocale(localeIdentifier: "en_US_POSIX")
utcDateFormatter.dateFormat = "yyyy-MM-dd HH:mm:ss"

let serverString = "2015-04-03 12:34:56" // date from server, as date representation in UTC
let date = utcDateFormatter.dateFromString(serverString) // date you got from server, as point in time

To display that server time to your user, you take that NSDate and put it into another dateFormatter:

// display absolute time from server as timezone aware string
let localDateFormatter = NSDateFormatter()
localDateFormatter.dateStyle = NSDateFormatterStyle.LongStyle
localDateFormatter.timeStyle = NSDateFormatterStyle.LongStyle
let localDateString = localDateFormatter.stringFromDate(date)

And if you want to send a date string back to your server, you take a NSDate and put it through your UTC dateFormatter.

// send back time to server
let dateString = utcDateFormatter.stringFromDate(NSDate())


来源:https://stackoverflow.com/questions/29406819/how-to-convert-server-response-date-to-local-timezone

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!