隐藏NSString以在不同时区进行NSDate [英] Covert NSString to NSDate in a different timezone
问题描述
我正在尝试将字符串转换为与当前系统时区不同的时区中的日期.我已将dateformatter的时区设置为此时区.但是我得到的NSDate始终在当前时区.
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.
例如,我当前的时区是PDT.我已经将DateFormatter的时区设置为CST.我在CST中有一个时间字符串.但是,当我使用日期格式化程序将此字符串转换为日期时,我在PDT中获得了日期,而我却希望在CST中获得它.
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.
有人可以帮忙吗?
谢谢.
推荐答案
NSDate
仅存储自2001年1月1日00:00:00以来的秒数.NSDate的描述将使用您当前的时区(例如,如果尝试使用NSLog进行调试).
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).
您必须使用
[NSTimeZone defaultTimeZone]
,然后使用来计算偏移量
and then calculate the offset by using
-(NSTimeInterval)secondsFromGMT
这篇关于隐藏NSString以在不同时区进行NSDate的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!