IPhone - How to get the time zone offset?

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

One problem with the following code is that it crashes with 4.3 simulator and works great with 4.2 simulator. Since 4.2 simulator gives output with reference to GMT simulator and 4.3 with reference to IST

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

I could not find a solution that is generic.

+4
source share
1 answer

I think you can use the NSDate and NSDateFormatter class to get the timezone offset. You can do it as follows.

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

So, in zoneOffset you will now have the necessary thing.

+5
source

All Articles