Objective-c格式时间一直到特定日期

Chr*_*ker 0 objective-c nsdate nstimeinterval

我正在创建一个倒数计时器,我需要打印剩余的时间(小时:分钟:秒)直到特定日期.我已经找到了如何获得Now和目标日期之间的时间间隔,但我不知道如何将时间间隔格式化为字符串.NSDateFormater是否适用于NSTimeInterval?

CRD*_*CRD 5

NSTimeInterval 以秒为单位,使用除法和余数来分解和格式化(代码未经测试):

NSString *timeIntervalToString(NSTimeInterval interval)
{
   long work = (long)interval; // convert to long, NSTimeInterval is *some* numeric type

   long seconds = work % 60;   // remainder is seconds
   work /= 60;                 // total number of mins
   long minutes = work % 60;   // remainder is minutes
   long hours = work / 60      // number of hours

   // now format and return - %ld is long decimal, %02ld is zero-padded two digit long decimal 
   return [NSString stringWithFormat:@"%ld:%02ld:%02ld", hours, minutes, seconds];
}
Run Code Online (Sandbox Code Playgroud)