代码之家  ›  专栏  ›  技术社区  ›  dpigera

iPhone:NSDate将GMT转换为本地时间

  •  9
  • dpigera  · 技术社区  · 15 年前

    我当前有一个API调用,它以以下格式返回日期/时间: 2010-10-10T07:54:01.878926

    我能假设这是格林尼治时间吗?我还将其转换为NSDate对象。有没有办法用本地的iPhone时间来重新计算时间?

    4 回复  |  直到 12 年前
        1
  •  29
  •   Iñigo Beitia    15 年前
    NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];
    dateFormatter.dateFormat = @"yyyy-MM-dd'T'HH:mm";
    
    NSTimeZone *gmt = [NSTimeZone timeZoneWithAbbreviation:@"GMT"];
    [dateFormatter setTimeZone:gmt];
    NSString *timeStamp = [dateFormatter stringFromDate:[NSDate date]];
    [dateFormatter release];
    
        2
  •  20
  •   nesimtunc    12 年前
    NSDate* localDateTime = [NSDate dateWithTimeInterval:[[NSTimeZone systemTimeZone] secondsFromGMT] sinceDate:pubDate];
    
        3
  •  1
  •   JFK    15 年前
    NSDate *sourceDate = [NSDate dateWithTimeIntervalSince1970:gmtTimestamp];
    NSTimeZone* destinationTimeZone = [NSTimeZone systemTimeZone];
    NSInteger sourceGMTOffset = [destinationTimeZone secondsFromGMTForDate:0];
    NSInteger destinationGMTOffset = [destinationTimeZone secondsFromGMTForDate:sourceDate];
    NSTimeInterval interval = destinationGMTOffset - sourceGMTOffset;
    NSDate* destinationDate = [[[NSDate alloc] initWithTimeInterval:interval sinceDate:sourceDate] autorelease];
    NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];
    dateFormatter.dateFormat = @"yyyy-MM-dd'T'HH:mm";
    [dateFormatter setTimeZone:destinationTimeZone];
    NSString *localTimeString = [dateFormatter stringFromDate:destinationDate];
    
        4
  •  -2
  •   DisplayName Je Wade Muchkla L    14 年前
    NSDate *now = [NSDate date];
    NSLog(@"%@", [now dateWithCalendarFormat:@"%Y-%m-%d %H:%M:%S" 
                  timeZone:[NSTimeZone timeZoneWithName:@"GMT"]]);
    
    推荐文章