计算日期间隔

NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init]; 
[dateFormatter setDateFormat:@"yyyy-MM-dd HH:mm"];
NSDate *date1 = [dateFormatter dateFromString:@"2012-1-1 11:00"]; 
NSDate *date2 = [dateFormatter dateFromString:@"2012-1-3 12:30"];
[dateFormatter release];
NSTimeInterval time = [date2 timeIntervalSinceDate:date1];
int days = ((int)time) / (3600 * 24); 
int hours = ((int)time) % (3600 * 24) / 3600;
int minutes = (((int)time) % (3600 * 24) % 3600) / 60;
NSString *string = [[NSString alloc] initWithFormat:@"%i天%i小时%i分钟", days, hours, minutes];
NSLog(@"%@", string);
[string release];

 

你可能感兴趣的:(ios,iPhone)