سؤال

I need to get the information of the difference between users timezone, to GMT London time, in hours, or minutes.

Anyone can help?

I've been looking around and i'm kind of confused.

Thanks,

rui

هل كانت مفيدة؟

المحلول

NSTimeZone has a method called secondsFromGMT.

Return Value
The current difference in seconds between the receiver and Greenwich Mean Time.

نصائح أخرى

I hope this helps you:

NSInteger hours, min;
NSDateFormatter *dateFormatterGMT = [[NSDateFormatter alloc] init];

//GMT Time Zone
NSTimeZone *gmt = [NSTimeZone timeZoneWithAbbreviation:@"GMT"];
[dateFormatterGMT setTimeZone:gmt];

dateFormatterGMT.dateFormat = @"h";
hours = [[dateFormatterGMT stringFromDate:[NSDate date]] intValue];
NSLog(@"%ld", hours);

dateFormatterGMT.dateFormat = @"mm";
min = [[dateFormatterGMT stringFromDate:[NSDate date]] intValue];
NSLog(@"%ld", min);

//User Time Zone
NSDateFormatter *dateFormatterUser = [[NSDateFormatter alloc] init];
NSDate *userDate = [NSDate date];

dateFormatterUser.dateFormat = @"h";
hours = [[dateFormatterUser stringFromDate:userDate] intValue];
NSLog(@"%ld", hours);

dateFormatterUser.dateFormat = @"mm";
min = [[dateFormatterUser stringFromDate:userDate] intValue];
NSLog(@"%ld", min);

[dateFormatterUser release];
مرخصة بموجب: CC-BY-SA مع الإسناد
لا تنتمي إلى StackOverflow
scroll top