2013-07-09 43 views
2

我的iPhone应用程序从网站下载汇率。它还会下载价格上次更新时间为EST。在我的应用程序中,我想将该时间与[NSDate date]进行比较。比较NSDate从互联网上下载的日期

基本上,我想用NSTimeInterval来比较下载时间与当前EST的差值。

我已浏览本网站并阅读文档,并且不确定如何实现此目的。据我所知,NSDate没有时区。它是GMT。所以我想将其转换为EST或将我的日期转换为GMT并比较两者。

这是我所下载的数据转换为日期:

NSDateComponents *dateComps = [[NSDateComponents alloc] init]; 
    [dateComps setHour:hour]; 
    [dateComps setMinute:minute]; 
    [dateComps setDay:day]; 
    [dateComps setMonth:month]; 
    [dateComps setYear:year]; 

    NSCalendar *calendar = [[NSCalendar alloc] initWithCalendarIdentifier:NSGregorianCalendar]; 

    self.lastUpdatedExchangeRateDate = [calendar dateFromComponents:dateComps]; 

我很欣赏你的去了解这一点的最好方式输入。

回答

1

应设置NSDateComponentstimeZone太:

dateComps.timeZone = [NSTimeZone timeZoneWithAbbreviation:@"EST"]; 

创建NSDate用正确的时区是很重要的,因为它代表在第二时间,并且没有时区的意识。

+0

哇。我没有意识到这会很简单,但我只需要在接收时设置时区即可。现在我想到了它是有道理的。感谢您指点我正确的方向。只有方法调用是[NSTimeZone timeZoneWithAbbreviation:@“EST”];我为你改变了它。 – Jamie