Instead of rolling your own string formatting code, you should be using an NSNumberFormatter
for numbers or an NSDateFormatter
for dates/times. These data formatters take care of localization of format to the user's locale and handle a variety of formats built-in.
For your use, you need to convert your millisecond time into an NSTimeInterval
(typedef
'd from a double):
NSTimeInterval time = rawtime/1e3;
Now you can use an NSDateFormatter
to present the time:
NSDate *timeDate = [NSDate dateWithTimeIntervalSinceReferenceDate:time];
NSString *formattedTime = [NSDateFormatter localizedStringFromDate:timeDate
dateStyle:NSDateFormatterNoStyle
timeStyle:NSDateFormatterMediumStyle];
NSString *rawTime = [[formattedTime componentsSeparatedByString:@" "] objectAtIndex:0];
on OS X where the last line removes the "AM/PM". This will work for any time less than 12 hrs and will give a formatted string in the localized format for HH:MM:SS. On the iPhone, localizedStringFromDate:dateStyle:timeStyle:
isn't available (yet). You can achieve the same effect with setTimeStyle:
, setDateStyle:
and stringFromDate:
on a date formatter instance.