你可以做這樣的事情:
- (NSString*)timeIntervalStringforDouble:(double)timeInterval {
NSString *[email protected]"";
NSInteger hours = (int)floor(timeInterval/3600.0);
double remainder = timeInterval-(hours*3600);
NSInteger minutes = (int)floor(remainder/60.0);
NSInteger seconds = (int)round(remainder-(minutes*60));
NSString *temporalSegment = @"hour";
if (hours) {
if (hours>1) temporalSegment = @"hours";
intervalString = [intervalString stringByAppendingFormat:@"%d %@ ",hours,temporalSegment];
}
if (minutes || hours) {
temporalSegment = (minutes==1)? @"minute":@"minutes";
intervalString = [intervalString stringByAppendingFormat:@"%d %@ ",minutes,temporalSegment];
}
if (seconds || minutes) {
temporalSegment = (seconds==1)[email protected]"second":@"seconds";
intervalString = [intervalString stringByAppendingFormat:@"%d %@",seconds, temporalSegment];
}
return intervalString;
}
砂金和格式的味道。
可能重複[如何將NSTimeInterval分解爲iPhone上的年,月,日,小時,分鐘和秒?](http://stackoverflow.com/questions/1237778/how-do-i-break -down-an-nstimeinterval-into-year-months-days-hours-minutes-an)或可能的[模糊日期算法](http://stackoverflow.com/questions/1052951/fuzzy-date-algorithm-in-jective -c) – 2012-08-13 19:05:46