查找时差的问题



在我的应用程序中,我想知道两者的区别:

一周总工作时数(54.30小时)

某一天的总工作时数(10.45小时)

例如,我想知道(54.30 hours - 10.45 hours)的值个小时。由于一周的总时数大于24小时,所以我无法转换为NSDate

使用这两个函数,这将有助于

下面的函数将返回小时字符串

中的总秒数
- (NSNumber *)secondsForTimeString:(NSString *)string {
    NSArray *components = [string componentsSeparatedByString:@"."];
    NSInteger hours   = [[components objectAtIndex:0] integerValue];
    NSInteger minutes = [[components objectAtIndex:1] integerValue];
    //NSInteger seconds = [[components objectAtIndex:2] integerValue];
   return [NSNumber numberWithInteger:(hours * 60 * 60) + (minutes * 60)];

}

下面的函数将从秒

返回格式化的时间
- (NSString *)timeFormatted:(int)totalSeconds {
    //int seconds = totalSeconds % 60;
    int minutes = (totalSeconds / 60) % 60;
    int hours = totalSeconds / 3600;
    return [NSString stringWithFormat:@"%02d.%02d",hours, minutes];
}

现在你可以像这样计算剩余的小时数:

int diffrence =[[self secondsForTimeString:@"54.30"] intValue] - [[self secondsForTimeString:@"10.45"] intValue];
NSLog(@"Remaining Hours - %@",[self timeFormatted:diffrence]);

最新更新