简体   繁体   中英

Compare NSDate to date downloaded from internet

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM