문제

My iPhone app downloads an exchange rate from a website. It also downloads the time that the rate was last updated in EST. In my app, I want to compare that time to the [NSDate date].

Basically, I want to compare the downloaded time to the current EST using NSTimeInterval to calculate the difference.

I have looked on this site and read through the docs and am uncertain on how to achieve this. As far as I can understand, NSDate is without time zone. It is GMT. So I want to convert that to EST or convert my date to GMT and compare the two.

This is how I convert my downloaded data to a date:

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];

I appreciate your input on the best way to go about this.

도움이 되었습니까?

해결책

You should set the NSDateComponents' timeZone too:

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

It's important to create the NSDate with the correct timezone, because it represents a second in time, and have no awareness of timezones.

라이센스 : CC-BY-SA ~와 함께 속성
제휴하지 않습니다 StackOverflow
scroll top