Время GMT на iPhone

Matisse Netbeans визуальный редактор является великим для разработки Swing.

8
задан cobbal 29 September 2009 в 04:18
поделиться

5 ответов

- (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;
}
-3
ответ дан 5 December 2019 в 08:24
поделиться

Если вы хотите отобразить его для целей отображения, используйте 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];
8
ответ дан 5 December 2019 в 08:24
поделиться

NSDate stores the time zone internally -- there are a few functions you can call and pass in a target timezone if you jsut want a string representation of the date, see apple's documentation

0
ответ дан 5 December 2019 в 08:24
поделиться

See:

CFTimeZoneCreateWithTimeIntervalFromGMT secondsFromGMT

Date & Time Programming Guide

0
ответ дан 5 December 2019 в 08:24
поделиться

Это более простая версия ответа Рамина

+ (NSDate *) GMTNow
{ 
 NSDate *sourceDate = [NSDate date];
    NSTimeZone* currentTimeZone = [NSTimeZone localTimeZone];
    NSInteger currentGMTOffset = [currentTimeZone secondsFromGMT];

    [sourceDate addTimeInterval:currentGMTOffset];

    return sourceDate;
}
9
ответ дан 5 December 2019 в 08:24
поделиться
Другие вопросы по тегам:

Похожие вопросы: