2012-12-27 69 views
2

在我的iPhone应用程序中,我想获得格林尼治标准时间的系统时区。是使用下面的代码。如何获得格林威治标准时间格式在iOS6

NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init]; 
    dateFormatter.dateFormat = @"zzz"; 
    [dateFormatter setTimeZone:[NSTimeZone systemTimeZone]]; 
    NSString *timeStamp = [dateFormatter stringFromDate:[NSDate date]]; 
    NSString *timeZoneString=[timeStamp substringFromIndex:3]; 
    [dateFormatter release]; 

我在iOS5中获取GMT格式的时区,但不是在iOS6中。如何在iOS6中以GMT格式获取时区?请帮忙!!!

+0

这个怎么样,'[dateFormatter setTimeZone:setTimeZone:[NSTimeZone timeZoneForSecondsFromGMT:0];'代替' [dateFormatter setTimeZone:[NSTimeZone systemTimeZone]];'?那样有用吗? – iDev

+0

你在iOS6中获得了什么? – Thilo

+0

可能abbreviationDictionary(NSTimeZone的类方法)正在导致您的问题。在ios5&ios6中检查日志记录的abbreviationDictionary,你会看到不同之处。但是你可以改变它。 –

回答

0

我修改了@ACB的代码,因为有几个拼写错误,请尝试[dateFormatter setTimezone:[NSTimeZone timeZoneForSecondsFromGMT:0]];而不是[dateFormatter setTimeZone:[NSTimeZone systemTimeZone]];。这似乎对我有用。

我想给@ACB这个答案的功劳。他/她想出了这个技巧,我只确认了它。

+0

感谢您的确认。我已添加为答案。这是我发布之前无法验证的猜测,因此添加为评论。 – iDev

+0

你想我擦除这个答案吗? –

+0

这实际上是你的愿望。无论如何谢谢你的确认。 – iDev

2

你可以检查一下,

[dateFormatter setTimeZone:[NSTimeZone timeZoneForSecondsFromGMT:0]]; 
代替

[dateFormatter setTimeZone:[NSTimeZone systemTimeZone]]; 
相关问题