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];


分享来源:
http://bbs.9ria.com/thread-25...

本文固定链接: http://www.js-code.com/ios/ios_53219.html