Ios NSDate返回5小时30公吨额外费用

Ios NSDate返回5小时30公吨额外费用,ios,objective-c,nsdate,nstimeinterval,Ios,Objective C,Nsdate,Nstimeinterval,我正在使用[NSDate date]获取当前日期,但它将返回5小时30 mts。它在下午前后返回不同的时间。我的要求是我想在1小时后删除缓存中的数据 目前,我正在使用以下代码来计算时差。 //storyDate是存储在缓存中的日期 NSTimeInterval timeGapInSeconds =fabs([[NSDate date] timeIntervalSinceDate:storyDate]); NSInteger daysGap= timeGapInSeconds/86400; NS

我正在使用[NSDate date]获取当前日期,但它将返回5小时30 mts。它在下午前后返回不同的时间。我的要求是我想在1小时后删除缓存中的数据

目前,我正在使用以下代码来计算时差。 //storyDate是存储在缓存中的日期

NSTimeInterval timeGapInSeconds =fabs([[NSDate date] timeIntervalSinceDate:storyDate]); 
NSInteger daysGap= timeGapInSeconds/86400;
NSInteger hoursGap = timeGapInSeconds/3600;
NSInteger mtsGap = timeGapInSeconds/60;

NSLog(@"days gap %lu, hours gap is %lu",(long)daysGap,(long)hoursGap);
NSLog(@"time gap in seconds %f",timeGapInSeconds);
 NSLog(@"time gap in mins %f",timeGapInSeconds/60);
NSLog(@"current date:%@",[NSDate date]);

如果要使其通用化,可以将其添加到
日期格式化程序中:

NSDateFormatter *dateFormatter = [[[NSDateFormatter alloc] init] autorelease];
[dateFormatter setLocale:[NSLocale currentLocale]];
或者,如果您正在为特定国家/地区提出任何申请,请使用以下信息:

NSLocale *usLoc = [[NSLocale alloc] initWithLocaleIdentifier:@"en_US"];
[dateFormatter setLocale:usLoc];

希望这能奏效

这是因为您的本地时区与记录日期时使用的GMT不同,不使用86400之类的常数进行日历计算(有些日子有23小时或25小时——想想夏时制吧!)。使用NSCalendar、NSDateComponents……这是因为您在印度。