计算两个时间的时间差(结果为秒)

// 1 时间1

NSString *timeOne = @"2014-07-29 09:51:50";

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

formatterDate.dateFormat = @"yyyy-MM-dd hh:mm:ss";

NSDate *purposeDate = [formatterDate dateFromString:timeOne];


// 2 时间2

NSDate *currentDate = [NSDate date];

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

formatter.dateFormat = @"yyyy-MM-dd hh:mm:ss";

NSString *timeTwo = [formatter stringFromDate:currentDate];


// 3 计算时间差

NSTimeInterval timeInterval = [currentDate timeIntervalSinceDate:purposeDate];


// 4 显示

NSString *message = [NSString stringWithFormat:@"1 time:%@\n 2 time:%@\n result:%d", timeOne, timeTwo, (int)timeInterval];

[[[UIAlertView alloc] initWithTitle:@"时间差计算" message:message delegate:nil cancelButtonTitle:nil otherButtonTitles:@"知道了", nil] show];


你可能感兴趣的:(计算两个时间的时间差(结果为秒))