Convert seconds into minutes and seconds

2019-01-14 07:19发布

How can I convert seconds into minutes and seconds?

I am aware that you can convert seconds to minutes like so, but I don't know how I can get the remainder of seconds...

int minutes = seconds / 60;

3条回答
老娘就宠你
2楼-- · 2019-01-14 08:02

Swift

/** Returns the seconds as in clock format 02:24  */
    class func formatMinuteSeconds(_ totalSeconds: Int) -> String {

        let minutes = Double(totalSeconds) / 60;
        let seconds = totalSeconds % 60;

        return String(format:"%02d:%02d", minutes, seconds);
    }
查看更多
SAY GOODBYE
3楼-- · 2019-01-14 08:04

You will obtain minutes with :

int minutes = totalSeconds / 60;

and remaining seconds with:

int seconds = totalSeconds % 60;.
查看更多
爷、活的狠高调
4楼-- · 2019-01-14 08:09

Here's a better answer from Convert Seconds Integer To HH:MM, iPhone

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

Swift version:

private func getFormattedVideoTime(totalVideoDuration: Int) -> (hour: Int, minute: Int, seconds: Int){
        let seconds = totalVideoDuration % 60
        let minutes = (totalVideoDuration / 60) % 60
        let hours   = totalVideoDuration / 3600
        return (hours,minutes,seconds)
    }
查看更多
登录 后发表回答