快速问题我使用以下代码检索两个时间戳之间的差异。 会议纪要现在给我0.5小时。 我怎么把它变成几分钟(NSInteger格式)?
NSDateFormatter* dateFormatter = [[[NSDateFormatter alloc] init] autorelease];
[dateFormatter setLocale:[[[NSLocale alloc] initWithLocaleIdentifier:@"en_US_POSIX"] autorelease]];
[dateFormatter setDateFormat:@"mm:ss"];
NSDate* firstDate = [dateFormatter dateFromString:@"06:00"];
NSDate* secondDate = [dateFormatter dateFromString:@"17:30"];
NSTimeInterval timeDifference = [secondDate timeIntervalSinceDate:firstDate];
我使用NSInteger来获取整个小时,但无法计算出会议记录......对我来说一定是太晚了: - /
NSInteger hoursBetweenDates = timeDifference / 60;
NSInteger minutesBetweenDates = ??;
NSLog(@"RAW: %f", timeDifference);
NSLog(@"Hours: %i", hoursBetweenDates);
NSLog(@"Minutes: %i", minutesBetweenDates);
谢谢你们! :-D
答案 0 :(得分:0)
NSTimeInterval以秒为单位。所以分钟是diff / 60.0,小时是diff / 3600.0。
答案 1 :(得分:0)
NSTimeInterval timeDifference = [secondDate timeIntervalSinceDate:firstDate];
int minute = (int)timeDifference % 3600 / 60;