IOS开发日期的比较

获取当前时间:

NSDate *  senddate=[NSDate date];  

NSDateFormatter  *dateformatter=[[NSDateFormatter alloc] init];  

[dateformatter setDateFormat:@"YYYYMMdd"];  

NSString *  locationString=[dateformatter stringFromDate:senddate];    

NSLog(@"locationString:%@",locationString);

 [dateformatter release]; 

 

日期的比较

  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-12-03 11:34  想吃天鹅肉的井底之蛙  阅读(286)  评论(0编辑  收藏  举报