您的位置:首页 > 其它

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

2014-09-30 16:15 405 查看
// 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];
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: