How to convert Jalali date to Gregorian date
-
I have a project where I need to interact with a MySQL database and manipulate dates. The problem, however, is that, as far as I can tell, MySQL only accepts dates in the
Gregorian calendar
. However, my users want to view and input dates in theJalali calendar
. To address this, I decided to make the dates viewable and editable in the Jalali date format but store and retrieve them in the Gregorian date format in the back-end.Assuming I have access to the year, month, and day, I can convert to Jalali like this:
void gregorianToJalali(int year, int month, int day) { QCalendar calendar(QCalendar::System::Jalali); QDate gregorianDate(year, month, day); QCalendar::YearMonthDay ymd = calendar.partsFromDate(gregorianDate); 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')); }
However, the problem is that I do not know how to convert from Jalali to Gregorian:
void jalaliToGregorian(int year, int month, int day) { QCalendar calendar(QCalendar::System::Gregorian); QDate jalaliDate(year, month, day); // This does not work, because QDate uses Gregorian dates by default. QCalendar::YearMonthDay ymd = calendar.partsFromDate(jalaliDate); 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')); }
Obviously, this does not work. I have two questions regarding this:
1- How do I convert between the two calendar types?
2- Is it better to have a VARCHAR field in MySQL to store Jalali dates, since MySQL does not natively support them? -
You should take a look at https://doc.qt.io/qt-6/qcalendar.html to convert from your calendar system to gregorian calendar.
-
@Christian-Ehrlicher I read the documentation multiple times before posting my question, but I find it difficult to understand. I would greatly appreciate it if you could point out what I might be doing wrong in my code. That’s why I took the time to ask for help.
-
I would go with https://doc.qt.io/qt-6/qcalendar.html#dateFromParts-1
-
@Christian-Ehrlicher said in How to convert Jalali date to Gregorian date:
I would go with https://doc.qt.io/qt-6/qcalendar.html#dateFromParts-1
That is exactly what I thought I should use as well. However, I’ve encountered a problem:
void jalaliToGregorian(int year, int month, int day) { QCalendar calendar(QCalendar::System::Gregorian); QDate gregorianDate = calendar.dateFromParts(year, month, day); qDebug() << QString("%1/%2/%3") .arg(gregorianDate.year(), 4, 10, QChar('0')) .arg(gregorianDate.month(), 2, 10, QChar('0')) .arg(gregorianDate.day(), 2, 10, QChar('0')); }
According to the code, I pass in a Jalali date
1403/10/15
. I expect to obtain the corresponding Gregorian date, which should be similar toJanuary 4, 2025
. Instead, I get this:"1403/10/15"
This result is obviously incorrect, but I can’t figure out why.
-
@hskoglund this did indeed work. and the results are correct:
"2025/01/04"
Thank you for your help.
Here is the code if anyone is curious:
void jalaliToGregorian(int year, int month, int day) { QCalendar calendar(QCalendar::System::Jalali); QDate gregorianDate = calendar.dateFromParts(year, month, day); qDebug() << QString("%1/%2/%3") .arg(gregorianDate.year(), 4, 10, QChar('0')) .arg(gregorianDate.month(), 2, 10, QChar('0')) .arg(gregorianDate.day(), 2, 10, QChar('0')); }
-
-