How to convert an NSTimeInterval (seconds) into minutes

前端 未结 12 990
轮回少年
轮回少年 2020-11-28 01:27

I\'ve got an amount of seconds that passed from a certain event. It\'s stored in a NSTimeInterval data type.

I want to convert it into

12条回答
  •  悲&欢浪女
    2020-11-28 01:44

    All of these look more complicated than they need to be! Here is a short and sweet way to convert a time interval into hours, minutes and seconds:

    NSTimeInterval timeInterval = 326.4;
    long seconds = lroundf(timeInterval); // Since modulo operator (%) below needs int or long
    
    int hour = seconds / 3600;
    int mins = (seconds % 3600) / 60;
    int secs = seconds % 60;
    

    Note when you put a float into an int, you get floor() automatically, but you can add it to the first two if if makes you feel better :-)

提交回复
热议问题