qtqdatetime

How to use QDateTime::fromString?


I now, the question sounds rather silly, but I just can't get it to work. Worst example:

QString time_format = "yyyy-MM-dd  HH:mm:ss";
QDateTime a = QDateTime::currentDateTime();
QString as = a.toString(time_format);

qDebug() << as; // print "2014-07-16  17:47:04"

QDateTime b;
b.fromString(as,time_format);
assert(b.isValid()); // fails

I create a valid QDatetime, make a string out of it (that is correct) and try to turn it into a QDatetime again (using the same time_format-string). But suddenly, the string can't be parsed.

Any ideas?


Solution

  • fromString is a static function that returns the date; so you need to do:

    QDateTime b = QDateTime::fromString(as,time_format);
    

    in your code b never chaged from its default initialized state