将秒转换为分钟和秒

max*_*ax_ 19 time objective-c

如何将秒转换为分钟和秒?

我知道你可以把秒转换成几分钟,但是我不知道如何才能得到剩下的秒数......

int minutes = seconds / 60;
Run Code Online (Sandbox Code Playgroud)

小智 65

您将获得分钟:

int minutes = totalSeconds / 60;
Run Code Online (Sandbox Code Playgroud)

剩下的秒数:

int seconds = totalSeconds % 60;.
Run Code Online (Sandbox Code Playgroud)

  • 当然,%是模运算符,它给出了一个数除以另一个数的余数.所以62/60 = 1,62%60 = 2,因为62 = 1*60 + 2 (3认同)

ace*_*des 27

这是从Convert Seconds Integer到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]; 
}
Run Code Online (Sandbox Code Playgroud)

Swift版本:

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)
    }
Run Code Online (Sandbox Code Playgroud)


hta*_*oya 5

迅速

/** 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);
    }
Run Code Online (Sandbox Code Playgroud)