Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt Extensions
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. Qt Development
  3. General and Desktop
  4. Is there a way i can get the difference between two QDates
Forum Updated to NodeBB v4.3 + New Features

Is there a way i can get the difference between two QDates

Scheduled Pinned Locked Moved General and Desktop
19 Posts 6 Posters 10.9k Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • D Offline
    D Offline
    deimos
    wrote on last edited by
    #2

    I think this is the way:

    @QDate today=QDate::currentDate();
    QDate ExpireDate;

    if ( ExpireDate.toJulianDay() - today.toJulianDay() < 8)
    ...@

    if you have ExpireDate as QString look at "QDate::fromString":http://doc.qt.nokia.com/latest/qdate.html#fromString

    1 Reply Last reply
    0
    • Z Offline
      Z Offline
      ZapB
      wrote on last edited by
      #3

      Just convert your strings to QDate objects using "QDate::fromString()":http://doc.qt.nokia.com/latest/qdate.html#fromString and then use "QDate::daysTo()":http://doc.qt.nokia.com/latest/qdate.html#daysTo.

      Shameless self-plug (and for Andre): For more complex manipulation of timespans see this "merge request":https://qt.gitorious.org/qt/qt/merge_requests/1014.

      Nokia Certified Qt Specialist
      Interested in hearing about Qt related work

      1 Reply Last reply
      0
      • V Offline
        V Offline
        varexnet32
        wrote on last edited by
        #4

        woow this is the best forums ever. once i will write the final code i will put it here

        1 Reply Last reply
        0
        • S Offline
          S Offline
          shreesh
          wrote on last edited by
          #5

          Hi all,

          I am trying to read date from Qstring and I want to extract only month from the date. Can any body tell me how to extract "month" only from the date?

          1 Reply Last reply
          0
          • S Offline
            S Offline
            shreesh
            wrote on last edited by
            #6

            Hi all,

            I am trying to read date from Qstring and I want to extract only month from the date. Can any body tell me how to extract "month" only from the date?

            1 Reply Last reply
            0
            • K Offline
              K Offline
              koahnig
              wrote on last edited by
              #7

              You can convert a string with "QDate::fromString":http://qt-project.org/doc/qt-5/qdate.html#fromString and you get the month through "month":http://qt-project.org/doc/qt-5/qdate.html#month

              Vote the answer(s) that helped you to solve your issue(s)

              1 Reply Last reply
              0
              • K Offline
                K Offline
                koahnig
                wrote on last edited by
                #8

                You can convert a string with "QDate::fromString":http://qt-project.org/doc/qt-5/qdate.html#fromString and you get the month through "month":http://qt-project.org/doc/qt-5/qdate.html#month

                Vote the answer(s) that helped you to solve your issue(s)

                1 Reply Last reply
                0
                • S Offline
                  S Offline
                  shreesh
                  wrote on last edited by
                  #9

                  Thank you for your effort in answering.
                  I am trying to put the code as mentioned below, but it throws me an error. Can you please help me in this?

                  @QString expt = qry->value(2).toString();
                  QDate extract(QDate::fromString(expt, "MM/dd/yyyy"));
                  int month = (QDate::month(extract)); //throws error here
                  ui->lineEdit_expt->setText(month);@

                  1 Reply Last reply
                  0
                  • S Offline
                    S Offline
                    shreesh
                    wrote on last edited by
                    #10

                    Thank you for your effort in answering.
                    I am trying to put the code as mentioned below, but it throws me an error. Can you please help me in this?

                    @QString expt = qry->value(2).toString();
                    QDate extract(QDate::fromString(expt, "MM/dd/yyyy"));
                    int month = (QDate::month(extract)); //throws error here
                    ui->lineEdit_expt->setText(month);@

                    1 Reply Last reply
                    0
                    • K Offline
                      K Offline
                      koahnig
                      wrote on last edited by
                      #11

                      @
                      QString expt = qry->value(2).toString();
                      QDate extract(QDate::fromString(expt, "MM/dd/yyyy"));
                      int month = extract.month();
                      ui->lineEdit_expt->setText(month);
                      @
                      That should work, but I have just changed your source and did not compile. So, plesae check.

                      In addition a friendly recommendation:
                      Qt is mainly based on C++. It is recommended to have a sound understanding of C++ basics and how to use it. In my opinion you would benefit from working through a "tutorial for C++":http://www.tutorialspoint.com/cplusplus/. There are many other tutorials like this around.

                      Vote the answer(s) that helped you to solve your issue(s)

                      1 Reply Last reply
                      0
                      • K Offline
                        K Offline
                        koahnig
                        wrote on last edited by
                        #12

                        @
                        QString expt = qry->value(2).toString();
                        QDate extract(QDate::fromString(expt, "MM/dd/yyyy"));
                        int month = extract.month();
                        ui->lineEdit_expt->setText(month);
                        @
                        That should work, but I have just changed your source and did not compile. So, plesae check.

                        In addition a friendly recommendation:
                        Qt is mainly based on C++. It is recommended to have a sound understanding of C++ basics and how to use it. In my opinion you would benefit from working through a "tutorial for C++":http://www.tutorialspoint.com/cplusplus/. There are many other tutorials like this around.

                        Vote the answer(s) that helped you to solve your issue(s)

                        1 Reply Last reply
                        0
                        • S Offline
                          S Offline
                          shreesh
                          wrote on last edited by
                          #13

                          Thank you, it worked.
                          I appreciate your suggestion.

                          1 Reply Last reply
                          0
                          • S Offline
                            S Offline
                            shreesh
                            wrote on last edited by
                            #14

                            Thank you, it worked.
                            I appreciate your suggestion.

                            1 Reply Last reply
                            0
                            • S Offline
                              S Offline
                              shreesh
                              wrote on last edited by
                              #15

                              I an application I am using the extracted month from Qdate and then to match it with "month" specified in a combobox. There are multiple database entries for a month, but in the output "tableview" only the last matching row is displaying. Below is the code I wrote.
                              Can somebody tell me if there is any mistake in the logic?

                              @QString filter_month = ui->comboBox_month->currentText();
                              QString filter_year = ui->comboBox_year->currentText();

                              conn.connopen();
                              QSqlQuery * qry = new QSqlQuery();
                              
                              qry->prepare("select class_date from schedule_class");
                              
                              if(qry->exec&#40;&#41;&#41;
                              {
                              
                              
                                  while(qry->next(&#41;)
                                  {
                                      QString expt = qry->value(0).toString();
                              
                                      QSqlQueryModel * modal = new QSqlQueryModel();
                              
                                      QDate extract(QDate::fromString(expt, "MM/dd/yyyy"));
                              
                                      int m = extract.month();
                                      QString month = QString::number(m);
                              
                                      int y = extract.year();
                                      QString year = QString::number(y);
                              
                                    if(month == filter_month and year == filter_year)
                                    {
                                    QSqlQuery * qry2 = new QSqlQuery();
                                    qry2->prepare("select * from schedule_class where class_date='"+expt+"'");
                                    qry2->exec&#40;&#41;;
                              
                                    modal->setQuery(* qry2);
                              
                                    }
                                  ui->tableView_classes->setModel(modal);
                                  }
                                  conn.connclose();
                              }
                              

                              @

                              1 Reply Last reply
                              0
                              • S Offline
                                S Offline
                                shreesh
                                wrote on last edited by
                                #16

                                I an application I am using the extracted month from Qdate and then to match it with "month" specified in a combobox. There are multiple database entries for a month, but in the output "tableview" only the last matching row is displaying. Below is the code I wrote.
                                Can somebody tell me if there is any mistake in the logic?

                                @QString filter_month = ui->comboBox_month->currentText();
                                QString filter_year = ui->comboBox_year->currentText();

                                conn.connopen();
                                QSqlQuery * qry = new QSqlQuery();
                                
                                qry->prepare("select class_date from schedule_class");
                                
                                if(qry->exec&#40;&#41;&#41;
                                {
                                
                                
                                    while(qry->next(&#41;)
                                    {
                                        QString expt = qry->value(0).toString();
                                
                                        QSqlQueryModel * modal = new QSqlQueryModel();
                                
                                        QDate extract(QDate::fromString(expt, "MM/dd/yyyy"));
                                
                                        int m = extract.month();
                                        QString month = QString::number(m);
                                
                                        int y = extract.year();
                                        QString year = QString::number(y);
                                
                                      if(month == filter_month and year == filter_year)
                                      {
                                      QSqlQuery * qry2 = new QSqlQuery();
                                      qry2->prepare("select * from schedule_class where class_date='"+expt+"'");
                                      qry2->exec&#40;&#41;;
                                
                                      modal->setQuery(* qry2);
                                
                                      }
                                    ui->tableView_classes->setModel(modal);
                                    }
                                    conn.connclose();
                                }
                                

                                @

                                1 Reply Last reply
                                0
                                • K Offline
                                  K Offline
                                  koahnig
                                  wrote on last edited by
                                  #17

                                  You should place a new topic.
                                  Go into a forum "eg Generanl and Desktop.":http://qt-project.org/forums/viewforum/10/ and press "Start new discussion"
                                  Choose describing a bit your problem. This will probably better attract readers for helping. "There are also the forum rules available":http://qt-project.org/wiki/ForumHelp

                                  Vote the answer(s) that helped you to solve your issue(s)

                                  1 Reply Last reply
                                  0
                                  • K Offline
                                    K Offline
                                    koahnig
                                    wrote on last edited by
                                    #18

                                    You should place a new topic.
                                    Go into a forum "eg Generanl and Desktop.":http://qt-project.org/forums/viewforum/10/ and press "Start new discussion"
                                    Choose describing a bit your problem. This will probably better attract readers for helping. "There are also the forum rules available":http://qt-project.org/wiki/ForumHelp

                                    Vote the answer(s) that helped you to solve your issue(s)

                                    1 Reply Last reply
                                    0
                                    • N Offline
                                      N Offline
                                      Nithya Somasundaram
                                      wrote on last edited by
                                      #19

                                      QDate cdat=QDate::currentDate();
                                      QString cur=QVariant(cdat.toJulianDay()).toString();
                                      QString exp=QVariant(QVariant(expdtcal).toDate().toJulianDay()).toString();
                                      int diff=QVariant(exp).toInt()-QVariant(cur).toInt();
                                      if((QVariant(expdtcal).toDate()).toJulianDay() - cdat.toJulianDay() <= 0)
                                      {
                                      //Don't add medicine
                                      qDebug()<<"Expired";
                                      }

                                      1 Reply Last reply
                                      0

                                      • Login

                                      • Login or register to search.
                                      • First post
                                        Last post
                                      0
                                      • Categories
                                      • Recent
                                      • Tags
                                      • Popular
                                      • Users
                                      • Groups
                                      • Search
                                      • Get Qt Extensions
                                      • Unsolved