문제

GMT 시간을 어떻게 얻습니까?

NSDate *c =[NSDate date];

GMT가 아닌 시스템 시간을 제공합니다.

도움이 되었습니까?

해결책

- (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) UTC (UTC)로 설정된 NSCALENDAR을 사용하여 후속 계산을 수행하는 경우 (+[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는 내부적으로 시간대를 저장합니다. jsut 날짜의 문자열 표현을 원한다면 대상 시간대로 전화를 걸고 통과 할 수있는 몇 가지 기능이 있습니다. 애플의 문서

보다:

cftimezonecreatewithTimeInterValfromgmt secondsfromgmt

날짜 및 시간 프로그래밍 안내서

라이센스 : CC-BY-SA ~와 함께 속성
제휴하지 않습니다 StackOverflow
scroll top