Is there a way i can get the difference between two QDates
-
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
@
-
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
-
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.
-
woow this is the best forums ever. once i will write the final code i will put it here
-
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?
-
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?
-
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
-
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
-
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);@ -
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);@ -
@
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. -
@
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. -
Thank you, it worked.
I appreciate your suggestion. -
Thank you, it worked.
I appreciate your suggestion. -
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()) { while(qry->next()) { 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(); modal->setQuery(* qry2); } ui->tableView_classes->setModel(modal); } conn.connclose(); }
@
-
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()) { while(qry->next()) { 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(); modal->setQuery(* qry2); } ui->tableView_classes->setModel(modal); } conn.connclose(); }
@
-
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 -
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 -
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";
}