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. [Solved] Question on QDate
Forum Updated to NodeBB v4.3 + New Features

[Solved] Question on QDate

Scheduled Pinned Locked Moved General and Desktop
3 Posts 2 Posters 1.9k Views 1 Watching
  • 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 Offline
    E Offline
    Endless
    wrote on last edited by
    #1

    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
    0
    • S Offline
      S Offline
      soroush
      wrote on last edited by
      #2

      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
      0
      • E Offline
        E Offline
        Endless
        wrote on last edited by
        #3

        Yes, that did it. Thanks for the help.

        1 Reply Last reply
        0

        • Login

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