[Solved] Question on QDate



  • 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?



  • 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");@



  • Yes, that did it. Thanks for the help.


Log in to reply
 

Looks like your connection to Qt Forum was lost, please wait while we try to reconnect.