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. How to convert Jalali date to Gregorian date
Forum Update on Monday, May 27th 2025

How to convert Jalali date to Gregorian date

Scheduled Pinned Locked Moved Solved General and Desktop
calendardate
7 Posts 3 Posters 576 Views
  • 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.
  • S Offline
    S Offline
    Saviz
    wrote on 12 Jan 2025, 00:00 last edited by
    #1

    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 the Jalali 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?

    1 Reply Last reply
    0
    • H Offline
      H Offline
      hskoglund
      wrote on 12 Jan 2025, 20:42 last edited by
      #6

      Try replacing that first line
      QCalendar calendar(QCalendar::System::Gregorian);
      with
      QCalendar calendar(QCalendar::System::Jalali);

      S 1 Reply Last reply 12 Jan 2025, 21:32
      2
      • C Online
        C Online
        Christian Ehrlicher
        Lifetime Qt Champion
        wrote on 12 Jan 2025, 06:40 last edited by
        #2

        You should take a look at https://doc.qt.io/qt-6/qcalendar.html to convert from your calendar system to gregorian calendar.

        Qt Online Installer direct download: https://download.qt.io/official_releases/online_installers/
        Visit the Qt Academy at https://academy.qt.io/catalog

        S 1 Reply Last reply 12 Jan 2025, 17:21
        1
        • C Christian Ehrlicher
          12 Jan 2025, 06:40

          You should take a look at https://doc.qt.io/qt-6/qcalendar.html to convert from your calendar system to gregorian calendar.

          S Offline
          S Offline
          Saviz
          wrote on 12 Jan 2025, 17:21 last edited by
          #3

          @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.

          1 Reply Last reply
          0
          • C Online
            C Online
            Christian Ehrlicher
            Lifetime Qt Champion
            wrote on 12 Jan 2025, 17:46 last edited by
            #4

            I would go with https://doc.qt.io/qt-6/qcalendar.html#dateFromParts-1

            Qt Online Installer direct download: https://download.qt.io/official_releases/online_installers/
            Visit the Qt Academy at https://academy.qt.io/catalog

            S 1 Reply Last reply 12 Jan 2025, 20:11
            0
            • C Christian Ehrlicher
              12 Jan 2025, 17:46

              I would go with https://doc.qt.io/qt-6/qcalendar.html#dateFromParts-1

              S Offline
              S Offline
              Saviz
              wrote on 12 Jan 2025, 20:11 last edited by
              #5

              @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 to January 4, 2025. Instead, I get this:

              "1403/10/15"
              

              This result is obviously incorrect, but I can’t figure out why.

              1 Reply Last reply
              0
              • H Offline
                H Offline
                hskoglund
                wrote on 12 Jan 2025, 20:42 last edited by
                #6

                Try replacing that first line
                QCalendar calendar(QCalendar::System::Gregorian);
                with
                QCalendar calendar(QCalendar::System::Jalali);

                S 1 Reply Last reply 12 Jan 2025, 21:32
                2
                • H hskoglund
                  12 Jan 2025, 20:42

                  Try replacing that first line
                  QCalendar calendar(QCalendar::System::Gregorian);
                  with
                  QCalendar calendar(QCalendar::System::Jalali);

                  S Offline
                  S Offline
                  Saviz
                  wrote on 12 Jan 2025, 21:32 last edited by Saviz 1 Dec 2025, 21:34
                  #7

                  @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'));
                  }
                  
                  1 Reply Last reply
                  0
                  • S Saviz has marked this topic as solved on 12 Jan 2025, 21:34
                  • S Saviz has marked this topic as solved on 12 Jan 2025, 21:34

                  1/7

                  12 Jan 2025, 00:00

                  • Login

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