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 578 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.
  • SavizS Offline
    SavizS Offline
    Saviz
    wrote on 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
    • hskoglundH Offline
      hskoglundH Offline
      hskoglund
      wrote on last edited by
      #6

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

      SavizS 1 Reply Last reply
      2
      • Christian EhrlicherC Offline
        Christian EhrlicherC Offline
        Christian Ehrlicher
        Lifetime Qt Champion
        wrote on 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

        SavizS 1 Reply Last reply
        1
        • Christian EhrlicherC Christian Ehrlicher

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

          SavizS Offline
          SavizS Offline
          Saviz
          wrote on 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
          • Christian EhrlicherC Offline
            Christian EhrlicherC Offline
            Christian Ehrlicher
            Lifetime Qt Champion
            wrote on 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

            SavizS 1 Reply Last reply
            0
            • Christian EhrlicherC Christian Ehrlicher

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

              SavizS Offline
              SavizS Offline
              Saviz
              wrote on 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
              • hskoglundH Offline
                hskoglundH Offline
                hskoglund
                wrote on last edited by
                #6

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

                SavizS 1 Reply Last reply
                2
                • hskoglundH hskoglund

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

                  SavizS Offline
                  SavizS Offline
                  Saviz
                  wrote on last edited by Saviz
                  #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
                  • SavizS Saviz has marked this topic as solved on
                  • SavizS Saviz has marked this topic as solved on

                  • Login

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