Convert Seconds Integer To HH:MM, iPhone

Objective CIphoneTimeFormatting

Objective C Problem Overview


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.

Objective C Solutions


Solution 1 - Objective C

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)
 }

Solution 2 - Objective C

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];
}

Solution 3 - Objective C

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];
}

Solution 4 - Objective C

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)"
    }
}

Solution 5 - Objective C

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
}()

Solution 6 - Objective C

For Swift with clock count

    var timer = NSTimer()
    var count = 1
    func updateTime() {
        count++
        let seconds = count % 60
        let minutes = (count / 60) % 60
        let hours = count / 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 {
            clockOutlet.text = "\(strHours):\(strMinutes):\(strSeconds)"
        }
        else {
            clockOutlet.text = "\(strMinutes):\(strSeconds)"
        }   
    }

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionStumfView Question on Stackoverflow
Solution 1 - Objective CRohit AgarwalView Answer on Stackoverflow
Solution 2 - Objective CedukuleleView Answer on Stackoverflow
Solution 3 - Objective CMongi ZaidiView Answer on Stackoverflow
Solution 4 - Objective CAksView Answer on Stackoverflow
Solution 5 - Objective CIsuruView Answer on Stackoverflow
Solution 6 - Objective CPerry CView Answer on Stackoverflow