一、在iOS中获取时间间隔并转换为时分秒格式:
```objc
-(NSString *)getCurrentTime {
NSDateFormatter *formatter = [[NSDateFormatter alloc] init];
[formatter setDateFormat:@"yyyy-MM-dd HH:mm:ss"];
NSString *dateTime = [formatter stringFromDate:[NSDate date]];
self.startTime = dateTime;
return startTime;
}
// 在开始计时的地方调用
[self getCurrentTime];
// 在结束时的方法里写如下代码
NSDateFormatter *formatter = [[NSDateFormatter alloc] init];
[formatter setDateFormat:@"yyyy-MM-dd HH:mm:ss"];
NSDate *date1 = [formatter dateFromString:startTime];
NSDate *date2 = [NSDate date];
NSTimeInterval aTimer = [date2 timeIntervalSinceDate:date1];
int hour = (int)(aTimer/3600);
int minute = (int)(aTimer - hour*3600)/60;
int second = aTimer - hour*3600 - minute*60;
NSString *dural = [NSString stringWithFormat:@"%d时%d分%d秒", hour, minute, second];
```
二、将秒数转换为分钟和秒的字符串格式:
```objc
-(NSString *)convertSecondsToMinutesAndSeconds:(double)seconds {
int totalMinutes = (int)(seconds / 60);
int remainingSeconds = (int)(seconds % 60);
NSString *minutesAndSeconds = [NSString stringWithFormat:@"%d:%02d", totalMinutes, remainingSeconds];
return minutesAndSeconds;
}
```
使用示例:
```objc
double seconds = 326.4;
NSString *result = [self convertSecondsToMinutesAndSeconds:seconds];
NSLog(@"%@", result); // 输出 "5:26"
以下是重构后的代码:
```objective-c
// 计算两个日期之间的分钟差
NSDate *startDate = ...; // 起始日期
NSDate *endDate = ...; // 结束日期
// 计算分钟差
NSInteger minutes = [startDate timeIntervalSinceDate:endDate] / (60.0 * 60.0);
NSInteger seconds = round([startDate timeIntervalSinceDate:endDate] % (60.0 * 60.0));
// 如果需要转换为天、月、星期等,可以使用 NSCalendar 和 NSDateComponents
NSCalendar *calendar = [NSCalendar currentCalendar];
NSDateComponents *components = [calendar components:(unitFlags | NSCalendarUnitMinute) fromDate:startDate toDate:endDate options:opts];
NSInteger days = [components day];
NSInteger months = [components month];
NSInteger weeks = [components weekOfYear];
// ... 其他单位的计算
```
这段代码首先计算了两个日期之间的分钟差,然后可以根据需要使用 `NSCalendar` 和 `NSDateComponents` 来计算其他时间单位。
// The time interval
NSTimeInterval theTimeInterval = 326.4; // Get the system calendar NSCalendar *sysCalendar = [NSCalendar currentCalendar]; // Create the NSDates
NSDate *date1 = [[NSDate alloc] init];
NSDate *date2 = [[NSDate alloc] initWithTimeInterval:theTimeInterval sinceDate:date1];
// Get conversion to months, days, hours, minutes unsigned int unitFlags = NSHourCalendarUnit | NSMinuteCalendarUnit | NSDayCalendarUnit | NSMonthCalendarUnit;
NSDateComponents *conversionInfo = [sysCalendar components:unitFlags fromDate:date1 toDate:date2 options:0];
NSLog(@"Conversion: %dmin %dhours %ddays %dmoths",[conversionInfo minute], [conversionInfo hour], [conversionInfo day], [conversionInfo month]);
[date1 release];
[date2 release];
下面是一个将时间间隔转换为小时、分钟和秒的简短而亲切的方式:
```objective
NSTimeInterval timeInterval = 326.4;
long seconds = lroundf(timeInterval); // Modulo (%) operator below needs int or long
int hour = seconds / 3600;
int mins = (seconds % 3600) / 60;
int secs = seconds % 60;
```
请注意,当你把一个浮点数转换为整数时,你得到的结果是自动完成的。但是,如果你愿意的话,你可以在结果前面添加一些数字以使其感觉更好。例如,将其设置为两位小数:
```objective
NSTimeInterval timeInterval = 326.4;
long seconds = lroundf(timeInterval * 100) / 100; // Modulo (%) operator below needs int or long
int hour = seconds / 3600;
int mins = (seconds % 3600) / 60;
int secs = seconds % 60;
```
这样可以确保得到的时间间隔格式更加友好。