Warning: file_get_contents(/data/phpspider/zhask/data//catemap/6/cplusplus/125.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C++ QDateTime:自动添加当前年份_C++_Qt_User Interface_Datetime - Fatal编程技术网

C++ QDateTime:自动添加当前年份

C++ QDateTime:自动添加当前年份,c++,qt,user-interface,datetime,C++,Qt,User Interface,Datetime,我需要从格式“hh:mm:ss”中获取一个QDateTime对象。从当前日期开始的年、月和日应取在哪里 除了在输入字符串中预先添加yyyy-mm-dd之外,是否有人建议使用更清洁的解决方案。您可以使用获取当前日期/时间,然后使用设置所需的时间: QDateTime dateTime= QDateTime::currentDateTime(); dateTime.setTime(QTime::fromString("11:11:11", "hh:mm:ss")); 编辑: 为了在输入字符串的格式

我需要从格式
“hh:mm:ss”
中获取一个
QDateTime
对象。从当前日期开始的年、月和日应取在哪里


除了在输入字符串中预先添加
yyyy-mm-dd
之外,是否有人建议使用更清洁的解决方案。

您可以使用获取当前日期/时间,然后使用设置所需的时间:

QDateTime dateTime= QDateTime::currentDateTime();
dateTime.setTime(QTime::fromString("11:11:11", "hh:mm:ss"));
编辑: 为了在输入字符串的格式为“MM/dd hh:MM:ss”时获取
QDateTime
对象,在将年份设置为当前年份时,我将执行以下操作:

QDateTime dateTime= QDateTime::fromString("12/17 11:11:11", "MM/dd hh:mm:ss");
//get current date
QDate currentDate= QDate::currentDate();
//get read date
QDate readDate= dateTime.date();
//assign new date by taking year from currentDate, month and days from readDate
dateTime.setDate(QDate(currentDate.year(), readDate.month(), readDate.day()));

请注意,当输入格式中没有字段时,将使用一些默认值。在这种情况下,年份设置为1900,直到最后一行将其分配给当前年份。

您可以使用获取当前日期/时间,然后使用设置所需的时间:

QDateTime dateTime= QDateTime::currentDateTime();
dateTime.setTime(QTime::fromString("11:11:11", "hh:mm:ss"));
编辑: 为了在输入字符串的格式为“MM/dd hh:MM:ss”时获取
QDateTime
对象,在将年份设置为当前年份时,我将执行以下操作:

QDateTime dateTime= QDateTime::fromString("12/17 11:11:11", "MM/dd hh:mm:ss");
//get current date
QDate currentDate= QDate::currentDate();
//get read date
QDate readDate= dateTime.date();
//assign new date by taking year from currentDate, month and days from readDate
dateTime.setDate(QDate(currentDate.year(), readDate.month(), readDate.day()));

请注意,当输入格式中没有字段时,将使用一些默认值。在这种情况下,年份被设置为1900,直到最后一行中它被分配到当前年份。

对于这个错误,我感到非常抱歉。如果字符串中也有“MM”,您有什么想法吗?@ff2,没关系,没问题:)您想放弃输入“MM”吗?或者您想将其设置为最终
QDateTime
对象的月份吗?实际上,我的应用程序的datetime格式为
“MM/dd hh:MM:ss”
。我想要它的日期时间和当前年份。按照你的方法,我需要丢弃
“MM/dd”
。不知道怎么做?目前我正在开始时追加年份并进行解析。@ff2,请查看我的编辑。就我个人而言,我会避免在输入字符串前加上当前年份,因为这是一个不必要的字符串操作。但我想,这两种解决方案都是可以的。我为这个错误感到非常抱歉。如果字符串中也有“MM”,您有什么想法吗?@ff2,没关系,没问题:)您想放弃输入“MM”吗?或者您想将其设置为最终
QDateTime
对象的月份吗?实际上,我的应用程序的datetime格式为
“MM/dd hh:MM:ss”
。我想要它的日期时间和当前年份。按照你的方法,我需要丢弃
“MM/dd”
。不知道怎么做?目前我正在开始时追加年份并进行解析。@ff2,请查看我的编辑。就我个人而言,我会避免在输入字符串前加上当前年份,因为这是一个不必要的字符串操作。但我想,两种解决方案都可以。