日期的比较

本文转载至 http://bbs.9ria.com/thread-258146-1-1.html

  1.日期可以进行比较以确定大小或相等,也可以确定两个日期之间的时间间隔。两个日期的间隔时间差可以使用-timeIntervalSinceDate:方法来计算
  

  1. NSDate * now = [NSDate date];
  2.   NSDate * anHourAgo = [now dateByAddingTimeInterval:-60*60];
  3.   NSTimeInterVal timeBetween = [now timeIntervalSinceDate:anHourAgo];
  4.   NSLog(@”%f”,timeBetween);
复制代码


  2.日期比较也可以使用-timeIntervalSinceNow方法获取和当前的时间间隔
  

  1. NSDate * anHourago = [NSDate dateWithTimeIntervalSinceNow;-60*60];
  2.   NSTimeInterval interval = [anHourAgo timeIntervalSinceNow];
  3.   NSLog(@”%f”,interval);
复制代码


  3.NSDate还提供了-laterDate、-earlierDate和compare方法来比较日期
  

  1. NSDate * now = [NSDate date];
  2.   NSDate * anHourAgo = [now dateByAddingTimeInterval:-60*60];
  3.   NSDate *result1 = [now laterDate:anHourAgo];
  4.   NSDate * result2 = [now earlierDate:anHourAgo];
  5.   NSComparisonResult result3 = [now compare:anHourAgo];
复制代码

 

posted @ 2015-03-13 15:22  天牛  阅读(386)  评论(0编辑  收藏  举报