如何将QT中的时间汇总到下一个小时?

如何将QT中的时间汇总到下一个小时?,qt,Qt,代码如下: startDate->setDateTime( QDateTime::currentDateTime() ); finishDate->setDateTime( QDateTime::currentDateTime() ); finishDate->setDateTime(startDate->dateTime().addSecs( 3600 )); 例如,现在的时间是13:34 我需要把开始时间四舍五入到14:00 完成时间也要四舍五入到15:00 有人知

代码如下:

startDate->setDateTime( QDateTime::currentDateTime() );
finishDate->setDateTime( QDateTime::currentDateTime() );
finishDate->setDateTime(startDate->dateTime().addSecs( 3600 ));
例如,现在的时间是13:34

我需要把开始时间四舍五入到14:00 完成时间也要四舍五入到15:00


有人知道怎么做吗?请再举个例子好吗?谢谢:D

假设您总是在最近的日期(不早于1970年)操作,您可以使用所谓的unixtime和简单数学,如下所示:

uint unixtime  = QDateTime::currentDateTime().toTime_t(); // current unix time
uint roundHour = unixtime + (3600 - unixtime % 3600); // round it up to an hour
QDateTime start;
QDateTime hourLater;
start.setTime_t(roundHour); // set start datetime to rounded time
hourLater.setTime_t(roundHour + 3600); // set finish time to one hour later
startDate->setDateTime(start);
finishDate->setDateTime(hourLater);

假设您总是在最近的日期(不早于1970年)操作,您可以使用所谓的unixtime和简单数学,如下所示:

uint unixtime  = QDateTime::currentDateTime().toTime_t(); // current unix time
uint roundHour = unixtime + (3600 - unixtime % 3600); // round it up to an hour
QDateTime start;
QDateTime hourLater;
start.setTime_t(roundHour); // set start datetime to rounded time
hourLater.setTime_t(roundHour + 3600); // set finish time to one hour later
startDate->setDateTime(start);
finishDate->setDateTime(hourLater);

我试过你的代码,我得到了以下错误:test.cpp:'classqdatetimeedit'没有名为'setTime\t'的成员test.cpp:62:error:'classqdatetimeedit'没有名为'setTime\t'的成员make:**[test.o]error 1$Oh,所以
startDate
finishDate
都是
QDateTimeEdit
。我以为他们只是
QDateTime
。让我更新一下这个例子。是的,你说得很对。没有注意到这一点,所以我删除了我的答案。谢谢你给我指点和+1。@navzy记住,如果答案解决了你的问题,就接受它。我更喜欢
roundHour=(unixtime+(3600-1))%3600在清晰性和所需操作的数量方面,但这些都是详细信息。我尝试了您的代码,但发现以下错误:test.cpp:'classqdatetimeedit'没有名为'setTime\t'的成员test.cpp:62:error:'classqdatetimeedit'没有名为'setTime\t'的成员make:**[test.o]错误1$Oh,因此
startDate
finishDate
QDateTimeEdit
。我以为他们只是
QDateTime
。让我更新一下这个例子。是的,你说得很对。没有注意到这一点,所以我删除了我的答案。谢谢你给我指点和+1。@navzy记住,如果答案解决了你的问题,就接受它。我更喜欢
roundHour=(unixtime+(3600-1))%3600在清晰性和所需操作的数量方面,但这些都是细节。