Qt Forum

    • Login
    • Search
    • Categories
    • Recent
    • Tags
    • Popular
    • Users
    • Groups
    • Search
    • Unsolved

    Update: Forum Guidelines & Code of Conduct

    [Solved] Question on QDate

    General and Desktop
    2
    3
    1627
    Loading More Posts
    • 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.
    • E
      Endless last edited by

      I want to translate a date in text format to a QDate variable. I looked at the help file, but it wasn't clear to me. Here's a code snippet:
      @
      QString dateString = "07/30/2012";
      QDate date = QDate::fromString(dateString);
      qDebug() << dateString << date;
      @

      The variable date is empty. What am I doing wrong?

      1 Reply Last reply Reply Quote 0
      • S
        soroush last edited by

        From documentation:

        @QDate QDate::fromString ( const QString & string, Qt::DateFormat format = Qt::TextDate ) [static]@

        bq. Returns the QDate represented by the string, using the format given, or an invalid date if the string cannot be parsed.
        bq. Note for Qt::TextDate: It is recommended that you use the English short month names (e.g. "Jan"). Although localized month names can also be used, they depend on the user's locale settings.

        @Qt::TextDate@

        bq. The default Qt format, which includes the day and month name, the day number in the month, and the year in full. The day and month names will be short, localized names. This is basically equivalent to using the date format string, "ddd MMM d yyyy". See QDate::toString() for more information.

        So, your string is in wrong format. Either use another string or another format:
        @QDate date = QDate::fromString(dateString,"dd/MM/yyyy");@

        1 Reply Last reply Reply Quote 0
        • E
          Endless last edited by

          Yes, that did it. Thanks for the help.

          1 Reply Last reply Reply Quote 0
          • First post
            Last post