2015-07-19 118 views
0

我有一個麻煩與QT qdatetime比較Qt和QDateTime比較

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"); 

我怎樣才能得到比較兩個日期的正確結果。感謝幫助!

+0

的方法是合適的。 ''nearest''在QDateTime :: fromString()'後面的時間表中保存了正確的值嗎? – techneaz

+0

我該如何檢查?如果我在消息框中顯示它,它會正確顯示。 –

+0

你可以爲你的問題添加一個'ui.timetableTable-> item(0,2) - > data(Qt :: DisplayRole).toString()'的示例值,你的意思是**總是顯示相同的數字** ??。如果'now'和'nearest'有效,QDateTime :: secsTo()'會給你一個有效的結果。 – techneaz

回答

1
  1. 使用QDateTime::fromString()時,將默認值分配給格式字符串中未提供的任何字段。默認值爲here

  2. 在您的情況下,當以年份格式傳遞"yy"時,年份的默認值爲1900 +在"yy"的字段中傳遞的值。 例子:

    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) 
    
  3. 您可以修改使用QDateTime::addYears()默認值。對於上面的例子:

    nearest = nearest.addYears(100);