获取iPhone中两个特定时区之间的时差?

时间:2012-03-30 08:44:55

标签: iphone

e.g。我想要 asia / kolkata 时间与 asia / dubai 之间的时差。

2 个答案:

答案 0 :(得分:8)

NSInteger differenceInSeconds = [timeZone1 secondsFromGMT] - [timeZone2 secondsFromGMT];

请注意,这会给出这些时区之间的当前时差。如果两个时区在一年中的不同时间观察到夏令时,则时差取决于您对此进行评估的时间。如果这对您很重要,您应该使用secondsFromGMTForDate:来获取特定日期的时区偏移量。

答案 1 :(得分:3)

NSDate *now = [NSDate date];
NSDateFormatter *df = [[NSDateFormatter alloc] init] ;
[df setTimeStyle:NSDateFormatterFullStyle];
[df setTimeZone:[NSTimeZone timeZoneWithName:Str]];//str = timezone1(for you its kolkata)
[df setDateFormat:@"dd-MM-yyyy HH:mm"];
NSString *S1 = [df stringFromDate:now];
[df setDateFormat:@"dd-MM-yyyy HH:mm"];
NSDate *dd = [df dateFromString:S1];
//dd = date1 as per timezone
NSLog(@"dd > %@",dd);

[df release];


NSDate *dt = [NSDate date];
NSLog(@"dt = %@",dt);
NSDateFormatter *df1 = [[NSDateFormatter alloc] init];
[df1 setTimeStyle:NSDateFormatterFullStyle];
[df1 setTimeZone:[NSTimeZone timeZoneWithName:Str2]];//str2=timezone2(for you its dubai)
[df1 setDateFormat:@"dd-MM-yyyy HH:mm"];

NSString *S2 = [df1 stringFromDate:dt];

[df setDateFormat:@"dd-MM-yyyy HH:mm"];
NSDate *dd1 = [df dateFromString:S2];
//dd1 = date2 as per timezone
NSLog(@"dd1 > %@",dd1);


NSCalendar *sysCalendar = [NSCalendar currentCalendar];
unsigned int unitFlags = NSHourCalendarUnit | NSMinuteCalendarUnit | NSDayCalendarUnit | NSMonthCalendarUnit;

NSDateComponents *conversionInfo = [sysCalendar components:unitFlags fromDate:dd  toDate:dd1  options:0];

NSLog(@"Conversion: %dmin %dhours %ddays %dmoths",[conversionInfo minute], [conversionInfo hour], [conversionInfo day], [conversionInfo month]);

我正在使用此代码并且它正常工作。

相关问题