Formatting seconds into hh:ii:ss

后端 未结 2 1980
心在旅途
心在旅途 2020-12-10 04:26

I have app that is a basic timer. It tracks the number of seconds the app has run. I want to convert it so the seconds (NSUInteger) are displayed like: 00:00:12 (hh:mm:ss).

2条回答
  •  暗喜
    暗喜 (楼主)
    2020-12-10 04:33

    This is similar to a previous answer about formatting time but doesn't require a date formatter because we aren't dealing with dates any more.

    If you have the number of seconds stored as an integer, you can work out the individual time components yourself:

    NSUInteger h = elapsedSeconds / 3600;
    NSUInteger m = (elapsedSeconds / 60) % 60;
    NSUInteger s = elapsedSeconds % 60;
    
    NSString *formattedTime = [NSString stringWithFormat:@"%u:%02u:%02u", h, m, s];
    

提交回复
热议问题