Convert seconds to days, minutes, and hours in Obj-c

后端 未结 11 2379
[愿得一人]
[愿得一人] 2020-12-13 01:16

In objective-c, how can I convert an integer (representing seconds) to days, minutes, an hours?

Thanks!

11条回答
  •  野趣味
    野趣味 (楼主)
    2020-12-13 01:59

    You will get days,hours,minutes and seconds from total seconds(self.secondsLeft)

    days = self.secondsLeft/86400;
    hours = (self.secondsLeft %86400)/3600;
    minutes = (self.secondsLeft % 3600) / 60;
    seconds = (self.secondsLeft %3600) % 60;
    

    Code ......

      [NSTimer scheduledTimerWithTimeInterval: 1.0 target:self selector:@selector(updateCountdown) userInfo:nil repeats: YES];
    
    -(void) updateCountdown {
    
        int days,hours,minutes,seconds;
        self.secondsLeft--;
    
        days = self.secondsLeft/86400;
        hours = (self.secondsLeft %86400)/3600;
        minutes = (self.secondsLeft % 3600) / 60;
        seconds = (self.secondsLeft %3600) % 60;
    
        NSLog(@"Time Remaining =%@",[NSString stringWithFormat:@"%02d:%02d:%02d:%02d",days, hours, minutes,seconds]);
    }
    

提交回复
热议问题