【发布时间】:2014-05-03 05:42:12
【问题描述】:
我正在使用 [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]);
【问题讨论】:
-
这是由于您的本地时区与您 NSLog 日期时使用的 GMT 不同
-
并且不要使用像 86400 这样的常量进行日历计算(有些日子是 23 或 25 小时——想想夏令时吧!)。使用 NSCalendar、NSDateComponents、...
-
那是因为你在印度。
标签: ios objective-c nsdate nstimeinterval