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.
  • V Offline
    V Offline
    varexnet32
    wrote on last edited by aha_1980
    #1

    hello guys i'm wrting a software wich gives the states of some cheques deppanding on the expiration dates. but i have to know first what the difference between the expiration date and the current date is .
    as example we are now 2011/4/30 and the cheque will expire in 2011/5/7 so how i can know that it will expire in 8 days.
    this is the code i'm using now but i noticed after tests that it has some bugs :
    @ QString ExpireDate;
    ExpireDate = ui->tableWidget->item(j,2)->text();
    QString ExpireYS = ExpireDate.split("/").at(0);
    QString ExpireMS = ExpireDate.split("/").at(1);
    QString ExpireDS = ExpireDate.split("/").at(2);
    int ExpireY = ExpireYS.toInt();
    int ExpireM = ExpireMS.toInt();
    int ExpireD = ExpireDS.toInt();
    QString stat;
    QDate LocalDate;
    LocalDate.setDate(LocalDate.currentDate().year(),LocalDate.currentDate().month(),LocalDate.currentDate().day());

        int DifY;
        int DifM;
        int DifD;
        DifY = ExpireY-LocalDate.year();
        if(DifY>0)
        {
            stat="green";
        }
        else if(DifY==0)
        {
            DifM = ExpireM-LocalDate.month();
            if(DifM>0)
            {
                stat="green";
            }
            else if(DifM==0)
            {
                DifD = ExpireD-LocalDate.day();
                if(DifD>10)
                {
                    stat="green";
                }
                else if(DifD<=10&&DifD>=5)
                {
                    stat="yellow";
                }
                else if(DifD<5)
                {
                    stat="red";
                }
            }
            else if(DifM<0)
            {
                stat="red";
            }
        }
        else if(DifY<0)
        {
            stat="red";
        }
    
        ui->tableWidget->item(j,3)->setText(stat);@
    

    please guys tell me if you can fix the code or maybe give me an easy way !

    edit : if some one will found it useful this is the final code (or something like the final code) which i'm using :
    @
    QDate today = QDate::currentDate();//this will get the current date on your system
    QDate ExpireDate;//you have to define the date of expiration by using something like setDate(int y,int m,int d)
    //after this you can simply obtain the stat as Qstring using a simple if() block
    QString stat;
    int leftDays = today.daysTo(ExpireDate);//this one gets the difference between 'today' and 'ExpireDate'

            if (leftDays > 10)
            {
                stat ="green";
            }
            else if (leftDays <= 10 && leftDays > 5)
            {
                stat = "yellow";
            }
            else if (leftDays < 5)
            {
                stat = "red";
            }      
            //now stat caontains : green or red or yellow            
    

    @

    1 Reply Last reply
    0
    • 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