How to get "Total Minutes" (TIMER)

Discussion in 'iOS Programming' started by Nekbeth, Apr 24, 2011.

  1. macrumors member

    Nekbeth

    Joined:
    Feb 20, 2011
    Location:
    Vatican City
    #1
    Hello, I'm trying to get the Total minutes for a Timer. I already got the seconds working. Here is how I got "Total Seconds":

    Code:
    NSCalendar *gregorian = [[NSCalendar alloc] initWithCalendarIdentifier:NSGregorianCalendar];
    
    
    
        NSDateComponents *comps = [gregorian components:(NSHourCalendarUnit | NSMinuteCalendarUnit) fromDate:time];
    
    
    
        NSInteger hour = [comps hour];
    
        NSInteger minute = [comps minute];
    
        NSLog(@"Hour:%i", hour);
    
        NSLog(@"minute:%i", minute);
    
        NSInteger secs =hour * 60 * 60 + minute * 60;
    
    
    
    
    
        NSNumber *elapsedSeconds = [[NSNumber alloc] initWithInt:secs];
    
        NSDictionary *myDict = [NSDictionary dictionaryWithObject:elapsedSeconds forKey:@"TotalSeconds"];


    It might be only to change the formula, any help would be much appreciated. Thanks
     
  2. Moderator emeritus

    robbieduncan

    Joined:
    Jul 24, 2002
    Location:
    London
    #2
    This may be massively simplistic but if you know the total elapsed seconds what is preventing you dividing that by 60 to get the number of minutes?
     
  3. thread starter macrumors member

    Nekbeth

    Joined:
    Feb 20, 2011
    Location:
    Vatican City
    #3
    You mean, something like :


    Code:
     NSInteger mins = secs / 60;
     NSNumber *elapsedMinutes = [[NSNumber alloc] initWithInt:mins];
    
    
      NSDictionary *myMin = [NSDictionary dictionaryWithObject:elapsedMinutes forKey:@"TotalMinutes"];
    ??
     
  4. Moderator emeritus

    robbieduncan

    Joined:
    Jul 24, 2002
    Location:
    London
    #4
    Assuming you only want the number of complete minutes then yes.
     
  5. thread starter macrumors member

    Nekbeth

    Joined:
    Feb 20, 2011
    Location:
    Vatican City
  6. Moderator emeritus

    robbieduncan

    Joined:
    Jul 24, 2002
    Location:
    London
    #6
    What makes you think my name is Duncan?
     
  7. thread starter macrumors member

    Nekbeth

    Joined:
    Feb 20, 2011
    Location:
    Vatican City
    #7
    I guess because of your nickname: robbieduncan
     
  8. Moderator emeritus

    robbieduncan

    Joined:
    Jul 24, 2002
    Location:
    London
    #8
    Yes. And Duncan is my surname. So it's weird to be referred to by it
     
  9. thread starter macrumors member

    Nekbeth

    Joined:
    Feb 20, 2011
    Location:
    Vatican City
    #9
    :) , is robbie ok ? * while you see this message, do you know how to make "Total Second" to reach 60 and loop again, instead of showing me "456 seconds remaing". *Minutes change if I set the scheduletime for 60, but only show up in my log until the first minute has passed. *

    (I'm using a date picker)

    Thanks
     
  10. Moderator emeritus

    robbieduncan

    Joined:
    Jul 24, 2002
    Location:
    London
    #10
    Yes :)

    Look at the modulus operator (%). For example 456 % 60 is 36.
     

Share This Page