开发者

iPhone - How to get time zone offset?

We have NSTimeZone class that tells more about the time zone. But I want to get only the offset of the time. For example: +05.30 is offset for Inida In other words offset of time with reference to UTC.

One problem with following code is that it 开发者_如何转开发crashes with 4.3 simulator and works fine with 4.2 simulator. Because 4.2 simulator is giving output with reference to GMT and 4.3 simulator with reference to IST

NSString* tzDescription = [[NSTimeZone systemTimeZone] description];
NSArray* tzArray = [tzDescription componentsSeparatedByString:@"GMT"];
NSString* gmtStr = [[tzArray objectAtIndex:1] substringToIndex:6];

I couldn't find a solution that is generic.


I think you can use NSDate and NSDateFormatter class to get the time zone offset. You can do it in following way.

NSDate *date = [NSDate date];
NSDateFormatter *dateFormat = [[NSDateFormatter alloc]
initWithDateFormat:@"zzz" allowNaturalLanguage:NO];
NSString *zoneOffset = [dateFormat stringFromDate:date];

So in zoneOffset now you will be having your required thing.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜