質問

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に設定された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はタイムゾーンを内部的に保存します - 日付の文字列表現が必要な場合は、ターゲットタイムゾーンで呼び出して渡すことができるいくつかの機能があります。 Appleのドキュメント

見る:

cftimezonecreatewithtimeIntervalfromgmt secondsfromgmt

日付と時刻のプログラミングガイド

ライセンス: CC-BY-SA帰属
所属していません StackOverflow
scroll top