-(NSString *)getCurrentTime
{
NSDateFormatter *formatter = [[NSDateFormatteralloc]init];
[formatter setDateFormat:@"yyyy-MM-dd HH:mm:ss"];
NSString *dateTime = [formatterstringFromDate:[NSDatedate]];
self.startTime = dateTime;
return startTime;
}
date1代表开始时间,在开始计时的地方调用 [self getCurrentTime]; 在结束时的方法里写如下代码:NSDateFormatter *formatter = [[NSDateFormatteralloc]init];
[formatter setDateFormat:@"yyyy-MM-dd HH:mm:ss"];
NSDate *date1 = [formatter dateFromString:startTime];
NSDate *date2 = [NSDatedate];
NSTimeInterval aTimer = [date2timeIntervalSinceDate:date1];
int hour = (int)(aTimer/3600);
int minute = (int)(aTimer -hour*3600)/60;
int second =aTimer -hour*3600 -minute*60;
NSString *dural = [NSStringstringWithFormat:@"%d时%d分%d秒", hour, minute,second];