How can I convert seconds into minutes and seconds?
I am aware that 开发者_如何学Cyou can convert seconds to minutes like so, but I don't know how I can get the remainder of seconds...
int minutes = seconds / 60;
You will obtain minutes with :
int minutes = totalSeconds / 60;
and remaining seconds with:
int seconds = totalSeconds % 60;.
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)
}
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);
}
You can resolve this problem with NSMeasurement
and NSUnitDuration
. These classes require iOS 10.0+, tvOS 10.0+, macOS 10.12+ or watchOS 3.0+. The code below converts seconds to minutes and seconds.
int seconds = 230;
NSMeasurement* measurement = [[NSMeasurement alloc] initWithDoubleValue:seconds unit:NSUnitDuration.seconds];
NSMeasurement* measurementMinutes = [measurement measurementByConvertingToUnit:NSUnitDuration.minutes];
int minutes = floor(measurementMinutes.doubleValue);
NSMeasurement* measurementMinutesWithoutSeconds = [measurement measurementBySubtractingMeasurement:[[NSMeasurement alloc] initWithDoubleValue:minutes unit:NSUnitDuration.minutes]];
int secondsRemainder = floor([measurementMinutesWithoutSeconds measurementByConvertingToUnit:NSUnitDuration.seconds].doubleValue);
NSLog(@"%d %d", minutes, secondsRemainder); // 3 50
Swift 5:
String with format hh:mm:ss
extension Float {
var hoursMinutesSecondsFormat : String {
let seconds = self.truncatingRemainder(dividingBy: 60).rounded(.toNearestOrAwayFromZero)
let minutes = (self / 60).truncatingRemainder(dividingBy: 60).rounded(.toNearestOrAwayFromZero)
let hours = (self / 3600).rounded(.toNearestOrAwayFromZero)
return String(format:"%02.f:%02.f:%02.f", hours,minutes, seconds);
}
}
DateComponentsFormatter: A formatter that creates string representations, by using unitsStyle u can get a string as you want and mention allowedUnits. e.g: output for unitsStyle:: for 10000 secods
full = "2 hours, 46 minutes, 49 seconds"
positional = "2:46:40"
abbreviated = "2h 46m 40s"
spellOut = "two hours, forty-six minutes, forty seconds”
short = "2hr,46 min,40 sec"
brief = "2hr 46min 40sec"
Easy to use:
let time = convertSecondsToHrMinuteSec(seconds: 10000) func convertSecondsToHrMinuteSec(seconds:Double) -> String{ let formatter = DateComponentsFormatter() formatter.allowedUnits = [.hour, .minute, .second] formatter.unitsStyle = .full let formattedString = formatter.string(from: TimeInterval(seconds))! print(formattedString) return formattedString }
精彩评论