Qt和QDateTime比较 [英] Qt and QDateTime comparing
问题描述
我有qt qdatetime比较麻烦
i have a trouble with qt qdatetime comparing
if(now.secsTo(nearest)>0)
总是显示相同的数字。
QDateTime now = QDateTime::currentDateTime();
QDateTime nearest = QDateTime::fromString(ui.timetableTable->item(0,2)->data(Qt::DisplayRole).toString(),"dd.MM.yy HH:mm");
如何获得比较两个日期的正确结果。感谢您的帮助!
how can i can i get the right result of comparing two dates. Thanks for help!
推荐答案
-
使用
QDateTime :: fromString()
,默认值分配给格式字符串中未提供的任何字段。默认值在此处提供。
When using
QDateTime::fromString()
,Default values are assigned to any field that is not provided in the format string. The default values are provided here.
在以yy
作为年份格式传递的情况下,年份的默认值为1900+传递的值在yy
的字段中。
示例:
In your case while passing "yy"
as a year format, the default value for the year is 1900 + the value passed in the field of "yy"
.
Example:
QDateTime nearest = QDateTime::fromString("02.07.15 12:15","dd.MM.yy
HH:mm");
qDebug()<< nearest; //Will give: QDateTime("1915-07-02 12:15:00.000)
您可以使用 QDateTime :: addYears()
修改默认值。对于上述示例:
You can modify the default value using QDateTime::addYears()
. For the above example:
nearest = nearest.addYears(100);
这篇关于Qt和QDateTime比较的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!