日期的比较
本文转载至 http://bbs.9ria.com/thread-258146-1-1.html
1.日期可以进行比较以确定大小或相等,也可以确定两个日期之间的时间间隔。两个日期的间隔时间差可以使用-timeIntervalSinceDate:方法来计算
- NSDate * now = [NSDate date];
- NSDate * anHourAgo = [now dateByAddingTimeInterval:-60*60];
- NSTimeInterVal timeBetween = [now timeIntervalSinceDate:anHourAgo];
- NSLog(@”%f”,timeBetween);
2.日期比较也可以使用-timeIntervalSinceNow方法获取和当前的时间间隔
- NSDate * anHourago = [NSDate dateWithTimeIntervalSinceNow;-60*60];
- NSTimeInterval interval = [anHourAgo timeIntervalSinceNow];
- NSLog(@”%f”,interval);
3.NSDate还提供了-laterDate、-earlierDate和compare方法来比较日期
- NSDate * now = [NSDate date];
- NSDate * anHourAgo = [now dateByAddingTimeInterval:-60*60];
- NSDate *result1 = [now laterDate:anHourAgo];
- NSDate * result2 = [now earlierDate:anHourAgo];
- NSComparisonResult result3 = [now compare:anHourAgo];