Время GMT на iPhone
Как получить время GMT?
NSDate *c =[NSDate date];
дает системное время, а не GMT.
7 ответов
- (NSDate*) convertToUTC:(NSDate*)sourceDate
{
    NSTimeZone* currentTimeZone = [NSTimeZone localTimeZone];
    NSTimeZone* utcTimeZone = [NSTimeZone timeZoneWithAbbreviation:@"UTC"];
    NSInteger currentGMTOffset = [currentTimeZone secondsFromGMTForDate:sourceDate];
    NSInteger gmtOffset = [utcTimeZone secondsFromGMTForDate:sourceDate];
    NSTimeInterval gmtInterval = gmtOffset - currentGMTOffset;
    NSDate* destinationDate = [[[NSDate alloc] initWithTimeInterval:gmtInterval sinceDate:sourceDate] autorelease]; 
    return destinationDate;
}
Это более простая версия ответа Рамина
+ (NSDate *) GMTNow
{ 
 NSDate *sourceDate = [NSDate date];
    NSTimeZone* currentTimeZone = [NSTimeZone localTimeZone];
    NSInteger currentGMTOffset = [currentTimeZone secondsFromGMT];
    [sourceDate addTimeInterval:currentGMTOffset];
    return sourceDate;
}
Если это для целей отображения, которые вы хотите отобразить, используйте NSDateFormatter следующим образом:
NSDate *myDate = [NSDate date];
NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];
[dateFormatter setTimeZone:[NSTimeZone timeZoneWithAbbreviation:@"GMT"]];
// Set date style:
[dateFormatter setDateStyle:NSDateFormatterShortStyle];
[dateFormatter setTimeStyle:NSDateFormatterShortStyle];
NSString *GMTDateString = [dateFormatter stringFromDate: myDate];
при выполнении вычислений дат эти категории могут быть полезны.  Преобразовав вашу дату в "нормализованную" (то есть с тем же месяцем, днем и годом, но с +1200 UTC), если вы затем выполните последующие вычисления с помощью NSCalendar, который также установлен в UTC (+[NSCalendar normalizedCalendar]), все получится.
@implementation NSDate (NormalizedAdditions)
+ (NSDate *)normalizedDateFromDateInCurrentCalendar:(NSDate *)inDate
{
    NSDateComponents *todayComponents = [[NSCalendar currentCalendar] components:(NSYearCalendarUnit | NSMonthCalendarUnit | NSDayCalendarUnit)
                                                                        fromDate:inDate];
    [todayComponents setTimeZone:[NSTimeZone timeZoneWithAbbreviation:@"UTC"]];
    [todayComponents setHour:12];
    return [[NSCalendar normalizedCalendar] dateFromComponents:todayComponents];
}
+ (NSDate *)normalizedDate
{
    return [self normalizedDateFromDateInCurrentCalendar:[NSDate date]];
}
@end
@implementation NSCalendar (NormalizedAdditions)
+ (NSCalendar *)normalizedCalendar
{
    static NSCalendar *gregorian = nil;
    if (!gregorian) {
        gregorian = [[NSCalendar alloc]
                     initWithCalendarIdentifier:NSGregorianCalendar];
        [gregorian setTimeZone:[NSTimeZone timeZoneWithAbbreviation:@"UTC"]];   
    }
    return gregorian;
}
@end
+ (NSDate*) convertToGMT:(NSDate*)sourceDate
{
    NSTimeZone* currentTimeZone = [NSTimeZone localTimeZone];
    NSTimeInterval gmtInterval = [currentTimeZone secondsFromGMTForDate:sourceDate];
    NSDate* destinationDate = [[[NSDate alloc] initWithTimeInterval:gmtInterval sinceDate:sourceDate] autorelease];     
    return destinationDate;
}
NSDate хранит часовой пояс внутри -- есть несколько функций, которые вы можете вызвать и передать в целевом часовом поясе, если вы хотите получить строковое представление даты, см. документация apple