有两种方式可以实现
方式一:
// 统一格式
NSDateFormatter *formater = [[NSDateFormatter alloc] init];
[formater setDateFormat:@"yyyy-MM-dd HH:mm"];
// create
NSArray *createTimeArray = [createDate componentsSeparatedByString:@"."];
createDate = [createTimeArray objectAtIndex:0];
NSDate *newCreateDate = [formater dateFromString:createDate];
NSTimeInterval createInterval = [newCreateDate timeIntervalSince1970] * 1;
// now
NSArray *nowTimeArray = [nowDate componentsSeparatedByString:@"."];
nowDate = [nowTimeArray objectAtIndex:0];
NSDate *newNowDate = [formater dateFromString:nowDate];
NSTimeInterval nowInterval = [newNowDate timeIntervalSince1970] * 1;
// 得到的时间差是秒数
NSTimeInterval timeDifference = nowInterval - createInterval;
float halfYear = [[NSNumber numberWithInt: kTVUHalfAYearInSeconds] floatValue];
NSLog(@"Jenny 1017 000 创建日期:%@\n 现在的日期:%@\n 中间的差值:%f\n",newCreateDate,newNowDate,timeDifference);
if (timeDifference - halfYear >= 0.000001) {
NSLog(@"The XML file was created for a distance of more than half a year and need to be deleted\n createDate:%@",createDate);
return YES;// 需要删除
}else{
NSLog(@"The XML file is created less than half a year and does not need to be deleted\n createDate:%@",createDate);
return NO;//不需要删除
}
// 统一格式
NSDateFormatter *formater = [[NSDateFormatter alloc] init];
[formater setDateFormat:@"yyyy-MM-dd HH:mm"];
NSDate *create = [formater dateFromString:createDate];
NSDate *now = [formater dateFromString:nowDate];
NSTimeInterval timeDifference = [now timeIntervalSinceDate:create];
float halfYear = [[NSNumber numberWithInt: kTVUHalfAYearInSeconds] floatValue];
NSLog(@"Jenny 1017 创建日期:%@\n 现在的日期:%@\n 中间的差值:%f\n",create,now,timeDifference);
if (timeDifference - halfYear >= 0.000001) {
NSLog(@"The XML file was created for a distance of more than half a year and need to be deleted\n createDate:%@",create);
return YES;// 需要删除
}else{
NSLog(@"The XML file is created less than half a year and does not need to be deleted\n createDate:%@",create);
return NO;//不需要删除
}