如何将服务器响应日期转换为本地时区?
在api调用响应中,我得到了不同时区的日期.我想将其转换为用户的本地时区,并且用户可以从ios时区列表中选择任何时区.这都是本地的.我们永远不会将选定的用户时区发送到服务器.
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.
在进行任何api调用时,可以说我正在选择器中以选定的开始日期创建一个事件.我应该将它发送给全球时区,而不是与用户选择的时区一起发送,因为它是应用程序本地的.
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.
当前,我在选择时更改defaultTimeZone,以便获得正确的当前日期等.然后将NSDateFormatter与setTimeZone一起使用... [NSTimeZone timeZoneWithName ...
Currently I am changing defaultTimeZone on selection so that I get proper current date etc. And using NSDateFormatter with setTimeZone ... [NSTimeZone timeZoneWithName...
这是更改所有在应用程序本地的defaultTimeZone的更好方法吗?将NSdate发送到服务器时,应如何将其转换为全局日期?
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
是一个时间点,您可以将其称为绝对时间.这个时间对于地球上的每个地方都是一样的. NSDate
并不表示格式化为特定位置人们喜欢的时间.这种差异非常重要.
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.
单个NSDate()可以在多个时区中具有多个表示形式.
A single NSDate() can have multiple representations in multiple timezones.
为了更加直观,这5个时钟都代表一个时间点(NSDate
实例):
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.
可以说这些时钟是NSDateFormatter.stringFromDate()
的时钟.他们将绝对时间点转换为本地表示.
One could say these clocks are what NSDateFormatter.stringFromDate()
is. They turn the absolute point in time into a local representation.
要在不同时区之间进行转换,请不要更改NSDate
,而应更改NSDateFormatter
的时区.如果从服务器获取的日期字符串使用UTC,则需要一个NSDateFormatter,它使用UTC作为timeZone:
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
要向您的用户显示该服务器时间,请将该NSDate放入另一个dateFormatter中:
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)
如果您想将日期字符串发送回服务器,请使用NSDate并将其通过UTC 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.
// send back time to server
let dateString = utcDateFormatter.stringFromDate(NSDate())