GMT time on iPhone

后端 未结 7 1108
清歌不尽
清歌不尽 2020-12-13 22:37

How do I get GMT time?

NSDate *c =[NSDate date];

gives system time, not GMT.

相关标签:
7条回答
  • 2020-12-13 22:58

    This is a simpler version of Ramin's answer

    + (NSDate *) GMTNow
    { 
     NSDate *sourceDate = [NSDate date];
        NSTimeZone* currentTimeZone = [NSTimeZone localTimeZone];
        NSInteger currentGMTOffset = [currentTimeZone secondsFromGMT];
    
        [sourceDate addTimeInterval:currentGMTOffset];
    
        return sourceDate;
    }
    
    0 讨论(0)
  • 2020-12-13 22:59
    + (NSDate*) convertToGMT:(NSDate*)sourceDate
    {
        NSTimeZone* currentTimeZone = [NSTimeZone localTimeZone];
    
        NSTimeInterval gmtInterval = [currentTimeZone secondsFromGMTForDate:sourceDate];
    
        NSDate* destinationDate = [[[NSDate alloc] initWithTimeInterval:gmtInterval sinceDate:sourceDate] autorelease];     
        return destinationDate;
    }
    
    0 讨论(0)
  • 2020-12-13 23:01

    If it's for display purposes you want to display it, use NSDateFormatter like so:

    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];
    
    0 讨论(0)
  • 2020-12-13 23:07
    - (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;
    }
    
    0 讨论(0)
  • 2020-12-13 23:09

    See:

    CFTimeZoneCreateWithTimeIntervalFromGMT secondsFromGMT

    Date & Time Programming Guide

    0 讨论(0)
  • 2020-12-13 23:18

    If performing date calculations, these categories may be useful. Having converted your date to being 'normalized' (that is, having the same month, day, and year but at +1200 UTC), if you then perform subsequent calculations using an NSCalendar that is also set to UTC (+[NSCalendar normalizedCalendar]), it'll all work out.

    @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
    
    0 讨论(0)
提交回复
热议问题