Convert gregorian date to persian date (jalali) in Qt

QDateConvertor class can help you to convert your date form geogorian to jalali and jalali to geogorian.

Edit: Add this class to your project and include "qdateconvertor.h" where you need to convert date.

   // gregorian to jalali 
   QDateConvertor mdate;
   QStringList shamsi=  mdate.ToJalali( "2001","9","11");
   QString JalailDate =shamsi.at(0)+"/"+shamsi.at(1)+"/"+shamsi.at(2)+ ":" +shamsi.at(3);
   qDebug()<<JalailDate;    
   // jalali to gregorian
   QStringList m= mdate.ToMiladi("1372","3","6");
   QString miladiDate= m.at(0)+"/"+ m.at(1)+"/"+m.at(2);
   qDebug()<<miladiDate;    

For QT >= 5.14 you can do like this:

QCalendar calendar(QCalendar::System::Jalali);

QDate today = QDate::currentDate();

QCalendar::YearMonthDay ymd = calendar.partsFromDate(today);

qDebug() << ymd.year;
qDebug() << ymd.month;
qDebug() << ymd.day;

qDebug() << QString("%1/%2/%3")
            .arg(ymd.year, 4, 10, QChar('0'))
            .arg(ymd.month, 2, 10, QChar('0'))
            .arg(ymd.day, 2, 10, QChar('0'));

Tags:

C++

Date

Qt