NSTimer 计算小时数

发布于 2024-09-03 19:45:34 字数 445 浏览 1 评论 0原文

我正在使用 NSTimer,我正在努力显示分钟和秒。但我对计算时间所需的数学感到困惑。

我正在使用:

- (void)updateCounter:(NSTimer *)theTimer {
    static int count = 0;
    count += 1;
    int seconds = count % 60;
    int minutes = (count - seconds) / 60;
    // Not sure how to calculate hours
    int hours = (count - minutes) / 60;
    self.timer.text = [NSString stringWithFormat:@"%.2d:%.2d:%.2d", hours, minutes, seconds];
}

我应该使用什么计算时间?

I am using an NSTimer which I have working to show minutes and seconds. But I am confused about the math needed to calculate hours.

I am using:

- (void)updateCounter:(NSTimer *)theTimer {
    static int count = 0;
    count += 1;
    int seconds = count % 60;
    int minutes = (count - seconds) / 60;
    // Not sure how to calculate hours
    int hours = (count - minutes) / 60;
    self.timer.text = [NSString stringWithFormat:@"%.2d:%.2d:%.2d", hours, minutes, seconds];
}

What calculation should I use for hours?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

国粹 2024-09-10 19:45:34
seconds = ...
seconds_in_minute = seconds % 60;
minutes_in_hour = ( seconds / 60 ) % 60;
hour_in_day = ( seconds / 3600 ) % 24;
seconds = ...
seconds_in_minute = seconds % 60;
minutes_in_hour = ( seconds / 60 ) % 60;
hour_in_day = ( seconds / 3600 ) % 24;
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文