简体   繁体   English

iOS:iPhone设备本地时区的时区问题

[英]iOS : Timezone Issue on Local timezone of iPhone device

I have been working on an application that takes dates from server in a certain format as given below. 我一直在研究一种应用程序,该应用程序以如下所示的某种格式从服务器获取日期。

"2015-02-03 00:00:00" “ 2015-02-03 00:00:00”

I want to show them with different UI format using NSDateFormatter but as i change the timezone it show previous day or sometimes next day as i changing timezone. 我想使用NSDateFormatter以不同的UI格式显示它们,但是当我更改时区时,它在更改时区时显示前一天或有时在第二天显示。 One important thing that server side date can be in any of timezone. 服务器端日期可以在任何时区中都是重要的一件事。 So, we don't know what kind of timezone date has. 因此,我们不知道哪种时区日期。 I need help to show this to the date as it is on server not change due to local timezone. 我需要帮助以显示此日期,因为本地时区服务器上的日期不会更改。

Thanks. 谢谢。

The simplest approach would be to parse it as if it were UTC - making it always valid, and without ever needing DST adjustments - and then format it in UTC as well. 最简单的方法是将其解析为UTC,以使其始终有效,而无需进行DST调整,然后将其格式化为UTC。 In other words, set the time zone of both the formatter and parser to UTC. 换句话说,将格式器和解析器的时区都设置​​为UTC。 For types which need a time zone, that's the simplest way of faking "There isn't a time zone here, just treat it all as local to an unspecified time zone." 对于需要时区的类型,这是最简单的伪装方式:“此处没有时区,只需将其全部视为未指定时区的本地即可。”

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM