-3
QString timestr("Fri Jan 5 14:03:11 CST 2018");
QDateTime time1 = QDateTime::fromString(timestr, "ddd MMM d hh:mm:ss CST yyyy");
qDebug() << time1.toString("yyyyMMdd");

I try to covert the string to an easier time description, however it failed. What could possibly be the problem with my QString?

y.leo
  • 9

1 Answers1

0

\note Unlike the other version of this function, day and month names must be given in the user's local language. It is only possible to use the English names if the user's language is English.

Note also this is the system locale, not the default locale you may have set in QLocale. To parse an English date string like this you will need to use QLocale::fromString() on an English language instance. So the following code will return correctly.

QLocale loc(QLocale::English);
    QString timestr("Fri Jan 5 14:03:11 CST 2018");
    QDateTime time1 = loc.toDateTime(timestr, "ddd MMM d hh:mm:ss CST yyyy");
    qDebug() << time1.toString("yyyyMMdd");

Hope it can help others with the same issue.

y.leo
  • 9