2

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?

1
  • Not to be cynical, but a great start would be to read the documentation built into Xcode. It is a very good resource.
    – Zak
    Feb 27, 2012 at 23:11

2 Answers 2

15

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.

2
  • While you CAN use NSDate for this (see netrace's answer) this is a much faster way of doing it.
    – lnafziger
    Feb 28, 2012 at 0:06
  • This way lacks localization support, so it is really basic
    – Nik
    Nov 5, 2013 at 10:13
3

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.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.