NSNumber of seconds to Hours, minutes, seconds

前端 未结 4 1056
情深已故
情深已故 2020-12-06 07:47

I am having a terrible time trying to do something that should be easy. I have a NSNumber value of 32025.89 seconds. I need to represent that in Hours, Minutes, Seconds. Is

4条回答
  •  甜味超标
    2020-12-06 08:22

    If you don't want to just divide it out, try this. It may be close enough for your purposes. Note: It doesn't account for sub-second precision. (setSecond takes an NSInteger).

    NSDateComponents* c = [[[NSDateComponents alloc] init] autorelease];
    [c setSecond:32025.89];
    
    NSCalendar* cal = [[[NSCalendar alloc] initWithCalendarIdentifier:NSGregorianCalendar]
                       autorelease];
    
    NSDate* d = [cal dateFromComponents:c];
    
    NSDateComponents* result = [cal components:NSHourCalendarUnit |
                                               NSMinuteCalendarUnit |
                                               NSSecondCalendarUnit
                                      fromDate:d];
    
    NSLog(@"%d hours, %d minutes, %d seconds",
        [result hour], [result minute], [result second]);
    

提交回复
热议问题