您的位置:首页 > 其它

NSDate计算两个时间日期的时差

2016-04-20 16:39 399 查看
小记一笔,免得下次翻阅的时候,又浪费太多时间去验证。

经常需要比对两个时间的差距,当差距7天的时候就给出一定的提示等等什么的。

代码如下:

- (BOOL)isOutOfDateTime {

//updateTime格式如下:2016-03-22

NSDateFormatter *format = [[NSDateFormatter alloc]init];
//根据updateTime的格式,写出对应的日期格式化串
[format setDateFormat:@"yyyy-MM-dd"];
[format setLocale:[NSLocale currentLocale]];
NSDate *currentDate = [format dateFromString:updateTime];

//获取当前的系统时间
NSDate *date = [NSDate date];
//消除8小时的误差。
NSTimeZone *zone = [NSTimeZone systemTimeZone];
NSInteger interval = [zone secondsFromGMTForDate:date];

//追加8小时
NSDate *localeDate = [date dateByAddingTimeInterval: interval];
currentDate = [currentDate dateByAddingTimeInterval:interval];
//计算时间差间隔
NSTimeInterval timeBetween = [localeDate timeIntervalSinceDate:currentDate];

//根据相差的秒数,看是否大于7天
if (timeBetween > 7 * 24 * 3600) {
return YES;
}
return NO;
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: