NSTimeInterval to NSDate

后端 未结 7 1508
逝去的感伤
逝去的感伤 2020-12-01 16:18

How can I convert a NSTimeInterval to NSDate? Think of it like a stopwatch. I want the initial date to be 00:00:00, and I have a NSTimeInterv

7条回答
  •  心在旅途
    2020-12-01 17:14

    I would advise against using NSDateFormatter if you wish to display time intervals. NSDateFormatter is useful when you wish to display times in the local, or a specific, timezone. But in this case it would be a bug if the time was timezone adjusted (e.g. one day per year has 23 hours).

    NSTimeInterval time = ...;
    NSString *string = [NSString stringWithFormat:@"%02li:%02li:%02li",
                                                  lround(floor(time / 3600.)) % 100,
                                                  lround(floor(time / 60.)) % 60,
                                                  lround(floor(time)) % 60];
    

提交回复
热议问题