how can i define NSTimeInterval to mm:ss format?
TimeInterval (née NSTimeInterval ) is a typealias for Double that represents duration as a number of seconds. You'll see it as a parameter or return type for APIs that deal with a duration of time.
A TimeInterval value is always specified in seconds; it yields sub-millisecond precision over a range of 10,000 years.
NSTimeInterval interval = 326.4;
long min = (long)interval / 60; // divide two longs, truncates
long sec = (long)interval % 60; // remainder of long divide
NSString* str = [[NSString alloc] initWithFormat:@"%02d:%02d", min, sec];
The %02d format specifier gives you a 2 digit number with a leading zero.
Note: this is for positive values of interval only.
See this question.
Accepted answer by Brian Ramsay is:
Given 326.4 seconds, pseudo-code:
minutes = floor(326.4/60)
seconds = round(326.4 - minutes * 60)
If you print with %02d
, you will get e.g. 03:08
if either number is less than 10.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With