Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Time in milliseconds to typical time format

I need to convert time collected in milliseconds (e.g. 116124) to typical format of time like this: 03:12:32:04.

I don't know how to simple do it... Could you help me?

like image 517
Konrad Kolasa Avatar asked Dec 26 '22 22:12

Konrad Kolasa


1 Answers

According to an Apple Dev forum linked here:

https://discussions.apple.com/thread/2350190?start=0&tstart=0

you have to do it yourself. Here is the function you can use that will return a formated string:

- (NSString *) formatInterval: (NSTimeInterval) interval{
unsigned long milliseconds = interval;
unsigned long seconds = milliseconds / 1000;
milliseconds %= 1000;
unsigned long minutes = seconds / 60;
seconds %= 60;
unsigned long hours = minutes / 60;
minutes %= 60;

NSMutableString * result = [NSMutableString new];

if(hours)
    [result appendFormat: @"%d:", hours];

[result appendFormat: @"%2d:", minutes];
[result appendFormat: @"%2d:", seconds];
[result appendFormat: @"%2d",milliseconds];

return result;
}
like image 70
Fellowsoft Avatar answered Jan 15 '23 06:01

Fellowsoft