iPhone 上的 GMT 时间

8

我如何获取GMT时间?

NSDate *c =[NSDate date];

该命令提供的是系统时间,而非格林威治标准时间。

7个回答

9

这是Ramin答案的简化版本

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

    [sourceDate addTimeInterval:currentGMTOffset];

    return sourceDate;
}

1
这是错误的。如果我在纽约,currentGMOffset将为-14400。由于GMT比纽约晚,我想要向sourceDate添加一个正值。此外,sourceDate在此处未被修改。您需要使用sourceDate = [sourceDate addTimeInterval:currentGMTOffset * -1];来反转偏移量的符号。 - ransomweaver
如果设备时间晚了2个小时,那么这个计算是否正确呢? - Shaik Riyaz

8

如果你想用于显示目的,可以使用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];

如果设备时间晚了2个小时,那么这个计算是否正确呢? - Shaik Riyaz
你如何知道设备晚了2个小时?你只能使用所提供的内容,并且必须依赖于系统时间在任何情况下都是正确的。这是系统的工作,而不是你的应用程序。 - Barry Hurley
我们必须考虑所有的情况,我的情况是我想要获取与设备时间无关的GMT时间...是否有其他方法可以实现这一点? - Shaik Riyaz
我认为你需要调用一些API来获取当前的GMT时间,这样无论使用哪个设备,你都能得到正确的时间。 - Suraj K Thomas

4

如果进行日期计算,这些类别可能会有用。将您的日期转换为“规范化”(也就是说,具有相同的月份、日子和年份,但在+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

2
+ (NSDate*) convertToGMT:(NSDate*)sourceDate
{
    NSTimeZone* currentTimeZone = [NSTimeZone localTimeZone];

    NSTimeInterval gmtInterval = [currentTimeZone secondsFromGMTForDate:sourceDate];

    NSDate* destinationDate = [[[NSDate alloc] initWithTimeInterval:gmtInterval sinceDate:sourceDate] autorelease];     
    return destinationDate;
}

1
这实际上是一个非常好的实用方法。我将在我的当前项目中使用它。但它并没有真正回答问题。问题是“如何获取GMT时间?” - Johan Karlsson

0

NSDate在内部存储时区 -- 如果你只想要日期的字符串表示,有一些函数可以调用并传入目标时区,详见苹果文档


0

-4
- (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;
}

2
-1 这种 NSDateNSDate 的转换是误导性的,并基于对 NSDate 对象表示的误解。 - albertamg
@Dave DeLong 我同意尝试在时区之间转换 NSDate 对象是无意义的,我不明白为什么这个其他答案会有那么多赞:https://dev59.com/8nNA5IYBdhLWcg3wH6EW#1082179 - albertamg
6
这个答案是错误的。NSDate对象代表一个特定的时间点,与时区无关。只有在格式化和显示日期时,时区才会起作用。修改NSDate对象意味着修改它所代表的时间点。 - Bryan Henry
如果设备时间晚了2个小时,那么这个计算是否正确呢? - Shaik Riyaz
在 Mavericks 中可用:- (NSDateComponents *)componentsInTimeZone:(NSTimeZone *)timezone fromDate:(NSDate *)date - the Reverend
显示剩余2条评论

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接