Covert NSString to NSDate in a different timezone

☆樱花仙子☆ 提交于 2019-12-10 22:47:37

问题


I am trying to convert a string to date in a separate time zone from the current system time zone. I have set the time zone of the dateformatter to this timezone. But the NSDate I get is always in my current time zone.

For instance, my current time zone is PDT. I have set the DateFormatter's time zone to CST. I have a string with the time in CST. But when I use date formatter to convert this string to date, I get the date in PDT, whereas I want it in CST.

Can someone please help.

Thanks.


回答1:


NSDate stores only the seconds since 00:00:00 01 Jan. 2001. The description of NSDate will use your current time zone (if try to debug with NSLog for example).

You must get the default time zone with

[NSTimeZone defaultTimeZone] 

and then calculate the offset by using

-(NSTimeInterval)secondsFromGMT


来源:https://stackoverflow.com/questions/910060/covert-nsstring-to-nsdate-in-a-different-timezone

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