Qt C++ Convert seconds to formatted string (hh:mm:ss)

时间:2022-06-28 02:49:17

As I said in title I need to convert seconds to hh:mm:ss

正如我在标题中所说,我需要将秒转换为hh:mm:ss

I tried this:

我试过这个:

 ui->label->setText(QDateTime::fromTime_t(10).toString("hh:mm:ss"));

But default value for hours is always 01 but I need it to be 00. As result I should get 00:00:10 but I get 01:00:10.

但是小时的默认值总是01但我需要它为00.结果我应该得到00:00:10但是我得到01:00:10。

2 个解决方案

#1


13  

Your timezone is included in it thats why. Try this:

您的时区包含在内,这就是原因。尝试这个:

QDateTime::fromTime_t(10).toUTC().toString("hh:mm:ss");

#2


3  

There is no QTime::fromTime_t; possibly you're using QDateTime::fromTime_t, which accounts for time zones and daylight savings.

没有QTime :: fromTime_t;可能你正在使用QDateTime :: fromTime_t,它考虑了时区和夏令时。

Instead you can use QTime().addSecs(10).toString(...).

相反,您可以使用QTime()。addSecs(10).toString(...)。

#1


13  

Your timezone is included in it thats why. Try this:

您的时区包含在内,这就是原因。尝试这个:

QDateTime::fromTime_t(10).toUTC().toString("hh:mm:ss");

#2


3  

There is no QTime::fromTime_t; possibly you're using QDateTime::fromTime_t, which accounts for time zones and daylight savings.

没有QTime :: fromTime_t;可能你正在使用QDateTime :: fromTime_t,它考虑了时区和夏令时。

Instead you can use QTime().addSecs(10).toString(...).

相反,您可以使用QTime()。addSecs(10).toString(...)。