简体   繁体   English

如何在 C++ 中将秒更改为 HH:MM:SS 格式?

[英]How to change seconds into HH:MM:SS format in C++?

So I have a function that returns the seconds between the range 0 and 86400 (24 hours).所以我有一个 function 返回范围 0 和 86400(24 小时)之间的秒数。 My goal is to take those seconds and return the hours, remaining minutes (between 0 and 60), and remaining seconds (between 0 and 60).我的目标是用这些秒数返回小时数、剩余分钟数(0 到 60 之间)和剩余秒数(0 到 60 之间)。

// What i have tried:

    hour = timeInSeconds / 3600;
    min = timeInSeconds / 60; // this is returning total minutes (which i dont want)
    sec = timeInSeconds % 60;

The main problem im having is with the minutes because they go above 60.我遇到的主要问题是分钟数,因为它们的 go 高于 60。

For example, if i make the time = 35000 seconds, i will get the hours to be 9 but my minutes will be around 853.例如,如果我使时间 = 35000 秒,我将得到 9 小时,但我的分钟将是 853 左右。

Therefore this is how my output looks like:因此,这就是我的 output 的样子:

17:0853:33 // How can i get remaining minutes? This returns to me total minutes.

So i guess, What im asking is how do i get the left over minutes from the total seconds after the hours have been calculated?所以我想,我要问的是如何在计算小时数后从总秒数中得到剩余的分钟数?

min = ((timeInSeconds % 3600) / 60)

Just needed to think a little bit more只是需要多想一点

hour = timeInSeconds / 3600;
min = (timeInSeconds - hour * 3600) / 60;
sec = timeInSeconds  - hour * 3600 - min * 60;

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM