NSDate额外返回5小时30分钟



我使用[NSDate date]来获取当前日期,但它正在返回5小时30分钟前。它在PM之前和PM之后返回不同的时间。我的要求是我想在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]);

如果您想使其一般化,可以将此添加到dateFormatter:

NSDateFormatter *dateFormatter = [[[NSDateFormatter alloc] init] autorelease];
[dateFormatter setLocale:[NSLocale currentLocale]];

或者,如果您正在为特定国家提出任何申请,则使用此:

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

希望这能起作用。

最新更新