How to convert an NSTimeInterval (seconds) into minutes

前端 未结 12 929
轮回少年
轮回少年 2020-11-28 01:27

I\'ve got an amount of seconds that passed from a certain event. It\'s stored in a NSTimeInterval data type.

I want to convert it into

相关标签:
12条回答
  • 2020-11-28 01:51

    Brief Description

    1. The answer from Brian Ramsay is more convenient if you only want to convert to minutes.
    2. If you want Cocoa API do it for you and convert your NSTimeInterval not only to minutes but also to days, months, week, etc,... I think this is a more generic approach
    3. Use NSCalendar method:

      • (NSDateComponents *)components:(NSUInteger)unitFlags fromDate:(NSDate *)startingDate toDate:(NSDate *)resultDate options:(NSUInteger)opts

      • "Returns, as an NSDateComponents object using specified components, the difference between two supplied dates". From the API documentation.

    4. Create 2 NSDate whose difference is the NSTimeInterval you want to convert. (If your NSTimeInterval comes from comparing 2 NSDate you don't need to do this step, and you don't even need the NSTimeInterval).

    5. Get your quotes from NSDateComponents

    Sample Code

    // The time interval 
    NSTimeInterval theTimeInterval = 326.4;
    
    // Get the system calendar
    NSCalendar *sysCalendar = [NSCalendar currentCalendar];
    
    // Create the NSDates
    NSDate *date1 = [[NSDate alloc] init];
    NSDate *date2 = [[NSDate alloc] initWithTimeInterval:theTimeInterval sinceDate:date1]; 
    
    // Get conversion to months, days, hours, minutes
    unsigned int unitFlags = NSHourCalendarUnit | NSMinuteCalendarUnit | NSDayCalendarUnit | NSMonthCalendarUnit;
    
    NSDateComponents *conversionInfo = [sysCalendar components:unitFlags fromDate:date1  toDate:date2  options:0];
    
    NSLog(@"Conversion: %dmin %dhours %ddays %dmoths",[conversionInfo minute], [conversionInfo hour], [conversionInfo day], [conversionInfo month]);
    
    [date1 release];
    [date2 release];
    

    Known issues

    • Too much for just a conversion, you are right, but that's how the API works.
    • My suggestion: if you get used to manage your time data using NSDate and NSCalendar, the API will do the hard work for you.
    0 讨论(0)
  • 2020-11-28 01:58

    Brian Ramsay’s code, de-pseudofied:

    - (NSString*)formattedStringForDuration:(NSTimeInterval)duration
    {
        NSInteger minutes = floor(duration/60);
        NSInteger seconds = round(duration - minutes * 60);
        return [NSString stringWithFormat:@"%d:%02d", minutes, seconds];
    }
    
    0 讨论(0)
  • 2020-11-28 02:00
        NSDate *timeLater = [NSDate dateWithTimeIntervalSinceNow:60*90];
    
        NSTimeInterval duration = [timeLater  timeIntervalSinceNow];
    
        NSInteger hours = floor(duration/(60*60));
        NSInteger minutes = floor((duration/60) - hours * 60);
        NSInteger seconds = floor(duration - (minutes * 60) - (hours * 60 * 60));
    
        NSLog(@"timeLater: %@", [dateFormatter stringFromDate:timeLater]);
    
        NSLog(@"time left: %d hours %d minutes  %d seconds", hours,minutes,seconds);
    

    Outputs:

    timeLater: 22:27
    timeLeft: 1 hours 29 minutes  59 seconds
    
    0 讨论(0)
  • 2020-11-28 02:02

    pseudo-code:

    minutes = floor(326.4/60)
    seconds = round(326.4 - minutes * 60)
    
    0 讨论(0)
  • 2020-11-28 02:03

    Swift 2 version

    extension NSTimeInterval {
                func toMM_SS() -> String {
                    let interval = self
                    let componentFormatter = NSDateComponentsFormatter()
    
                    componentFormatter.unitsStyle = .Positional
                    componentFormatter.zeroFormattingBehavior = .Pad
                    componentFormatter.allowedUnits = [.Minute, .Second]
                    return componentFormatter.stringFromTimeInterval(interval) ?? ""
                }
            }
        let duration = 326.4.toMM_SS()
        print(duration)    //"5:26"
    
    0 讨论(0)
  • 2020-11-28 02:04

    Forgive me for being a Stack virgin... I'm not sure how to reply to Brian Ramsay's answer...

    Using round will not work for second values between 59.5 and 59.99999. The second value will be 60 during this period. Use trunc instead...

     double progress;
    
     int minutes = floor(progress/60);
     int seconds = trunc(progress - minutes * 60);
    
    0 讨论(0)
提交回复
热议问题