在iOS中添加两个HH:mm:ss

时间:2014-09-25 10:27:11

标签: ios objective-c

我试图查找所有可能的答案,但找不到如何以“HH:mm:ss”格式添加两次。

我在数组中有几次捕获的时间,我想以HH:mm:ss格式返回所有的总和 这是我的代码:

-(void) addTime
{

   NSMutableArray *recordedArray = [[NSMutableArray alloc] initWithObjects:@"00:00:24",@"00:08:12",@"00:09:36",@"01:30:25", nil];

   double sumOne = 0;

   for (NSNumber * n in recordedArray)
   {
     sumOne += [n doubleValue];
   }

   NSLog(@"Sum of array time in HH:mm:ss is--> %f", sumOne);
}

2 个答案:

答案 0 :(得分:1)

我明白了。做的很少但值得。请在使用前优化代码。

    -(void) addTime

 {

    NSMutableArray *recordedArray = [[NSMutableArray alloc] initWithObjects:@"00:00:24",@"00:08:12",@"00:09:36",@"01:30:25", nil];

    captureAllSecondsArray = [[NSMutableArray alloc] init];// Define NSMutableArray * captureAllSecondsArray in your .h file.

    for (int i = 0; i<[recordedArray count]; i++)
    {
      [captureAllSecondsArray addObject:[self dateToSecondConvert:[recordedArray objectAtIndex:i]]];
    }


     int sumOfArray = 0;
     for (NSNumber * n in captureAllSecondsArray)
     {
       sumOfArray += [n intValue];
     }

     NSString *iCanUseThis= [NSString stringWithFormat:@"%@", [self timeFormatted:sumOfArray]];

     NSLog(@"Sum of array time in HH:mm:ss is--> %@",iCanUseThis);


 }

 - (NSNumber *)dateToSecondConvert:(NSString *)string
  {

    NSArray *components = [string componentsSeparatedByString:@":"];

    NSInteger hours   = [[components objectAtIndex:0] integerValue];
    NSInteger minutes = [[components objectAtIndex:1] integerValue];
    NSInteger seconds = [[components objectAtIndex:2] integerValue];

    NSNumber *secondsNumber = [NSNumber numberWithInteger:(hours * 60 * 60) + (minutes * 60) + seconds];

    return secondsNumber;
  }

- (NSString *)timeFormatted:(int)totalSeconds
 {

   int seconds = totalSeconds % 60;
   int minutes = (totalSeconds / 60) % 60;
   int hours = totalSeconds / 3600;

   NSString *totalTime = [NSString stringWithFormat:@"%02d:%02d:%02d",hours, minutes, seconds];

   return totalTime;
 }

答案 1 :(得分:-1)

NSMutableArray *arrnumberofoffer = [[NSMutableArray alloc] initWithObjects:@"00:00:24",@"00:08:12",@"00:09:36",@"01:30:25", nil];
NSArray *arrtimeremaing =[[NSArray alloc] init];
int hours,mins,sec,secondsLeft;
secondsLeft=0;
NSLog(@"%d",arrnumberofoffer.count);
for (int i =0 ; i<[arrnumberofoffer count]; i++) {
    NSString *str2 =[arrnumberofoffer objectAtIndex:i];

    //time seperated by :
    arrtimeremaing =[str2 componentsSeparatedByString: @":"];

    //convert the string value into int
    NSString *hrstr=[arrtimeremaing objectAtIndex:0];
    hours = [hrstr intValue];
    NSString *minstr=[arrtimeremaing objectAtIndex:1];
    mins = [minstr intValue];
    NSString *secstr=[arrtimeremaing objectAtIndex:2];
    sec = [secstr intValue];

    hours = hours*3600;
    mins = mins*60;
    NSLog(@"hors%d",hours);
    NSLog(@"min =%d",mins);
    NSLog(@"sec == %d",sec);

    //convert the time into second
    secondsLeft =secondsLeft +hours+mins+sec;
}
NSLog(@"%d",secondsLeft);
hours =(secondsLeft/3600);
mins = (secondsLeft/ 60) % 60;
sec = secondsLeft % 60;

NSString *str =[NSString stringWithFormat:@"%d:%d:%d",hours,mins,sec];
NSLog(@"%@",str);

试试这段代码。 如果答案是正确的,那么接受它并进行投票。