Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

how can i define NSTimeInterval to mm:ss format in iphone?

how can i define NSTimeInterval to mm:ss format?

like image 779
iOS_User Avatar asked Apr 01 '10 08:04

iOS_User


People also ask

What is NSTimeInterval?

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.

Is time interval in seconds Swift?

A TimeInterval value is always specified in seconds; it yields sub-millisecond precision over a range of 10,000 years.


2 Answers

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.

like image 167
progrmr Avatar answered Oct 06 '22 00:10

progrmr


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.

like image 28
Chris Cooper Avatar answered Oct 06 '22 01:10

Chris Cooper