Convert Seconds Integer To HH:MM, iPhone

I am struggling with this. I have a value in seconds that I want to display in a label in HH:MM format. I have searched the internet for ages and found some answers, but either not fully understood them, or they seem like an odd way of doing what I want. If someone could help me out on this one that would be great! Bear in mind that I am new to this games so this question may seem like a really basic one to the more experienced out there.


I was looking for the same thing that you are looking but couldn't find one. So I wrote one -

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

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

    return [NSString stringWithFormat:@"%02d:%02d:%02d",hours, minutes, seconds]; 
}

works perfectly in Swift as well:

 func timeFormatted(totalSeconds: Int) -> String {
    let seconds: Int = totalSeconds % 60
    let minutes: Int = (totalSeconds / 60) % 60
    let hours: Int = totalSeconds / 3600
    return String(format: "%02d:%02d:%02d", hours, minutes, seconds)
 }

In iOS 8.0 and higher versions it can also be done with NSDateComponentsFormatter. I need to mention that it will format the string without first leading zero, for example '9:30', but not '09:30'. But if you like to use formatters, you can use this code:

-(NSString *)getTimeStringFromSeconds:(double)seconds
{
     NSDateComponentsFormatter *dcFormatter = [[NSDateComponentsFormatter alloc] init];
     dcFormatter.zeroFormattingBehavior = NSDateComponentsFormatterZeroFormattingBehaviorPad;
     dcFormatter.allowedUnits = NSCalendarUnitHour | NSCalendarUnitMinute;
     dcFormatter.unitsStyle = NSDateComponentsFormatterUnitsStylePositional;
     return [dcFormatter stringFromTimeInterval:seconds];
}

This is how I do it:

-(NSString *)formatTimeFromSeconds:(int)numberOfSeconds
{

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

    //we have >=1 hour => example : 3h:25m
    if (hours) {
        return [NSString stringWithFormat:@"%dh:%02dm", hours, minutes];
    }
    //we have 0 hours and >=1 minutes => example : 3m:25s
    if (minutes) {
        return [NSString stringWithFormat:@"%dm:%02ds", minutes, seconds];
    }
    //we have only seconds example : 25s
    return [NSString stringWithFormat:@"%ds", seconds];
}

For Swift:

func formatTimeInSec(totalSeconds: Int) -> String {
    let seconds = totalSeconds % 60
    let minutes = (totalSeconds / 60) % 60
    let hours = totalSeconds / 3600
    let strHours = hours > 9 ? String(hours) : "0" + String(hours)
    let strMinutes = minutes > 9 ? String(minutes) : "0" + String(minutes)
    let strSeconds = seconds > 9 ? String(seconds) : "0" + String(seconds)

    if hours > 0 {
        return "\(strHours):\(strMinutes):\(strSeconds)"
    }
    else {
        return "\(strMinutes):\(strSeconds)"
    }
}

While @Aks' answer for Swift works, I'm a little skeptic about hardcoding values in my code. @edukulele's answer is much more cleaner but it's in Objective-C. I translated it to Swift with a slight change. I normally write my formatters as lazy vars.

private lazy var dateFormatter: NSDateComponentsFormatter = {
    let formatter = NSDateComponentsFormatter()
    formatter.zeroFormattingBehavior = .Pad
    formatter.allowedUnits = [.Hour, .Minute, .Second]
    formatter.unitsStyle = .Positional
    return formatter
}()