I want to go from minutes as an int
(eg 92) to a string of 1:32
. Is there a fancy way to do this using objc
or I'm I stuck with figuring it out the old-fashioned way?
-
Not to be cynical, but a great start would be to read the documentation built into Xcode. It is a very good resource.– ZakFeb 27, 2012 at 23:11
2 Answers
I do not believe there is a function to do this, but it is simple enough to do on your own.
int hour = minutes / 60;
int min = minutes % 60;
NSString *timeString = [NSString stringWithFormat: %@"%d:%02d", hour, min];
The '02' will pad the results so there are always two digits in the minutes place. It may be useful to look at the NSDate Class Reference as it may save you time if you want to do calculations on the result.
-
While you CAN use NSDate for this (see netrace's answer) this is a much faster way of doing it. Feb 28, 2012 at 0:06
-
I think you can find the answer here: NSTimeInterval to NSDate
The int
you talking about should be NSTimeInterval
(that is just double
), convert it to a NSDate
object and then format it using the NSDateFormatter
with: HH:mm
.
It can also could be more useful "extend" using categories NSDate object
@interface NSDate (Minutes)
and put there a method convert minutes to HH:mm (maybe could be better to move the format string as method parameter) so you never more need think about it.