Solved QDateTime fromString () does not work
-
I am trying to get a DATETIME from an sql server and the formatting is being yyyy-MM-dd hh:mm:ss but an invalid date is returned.
This is the value returned by query 2019-06-14T08:43:48.000, also inserts a timestamp manually as shown below but the value remains invalid
QDateTime Vendas::feitaEm() const { QDateTime dt; dt.fromString("2019-06-16 09:45:05", "yyyy-MM-dd hh:mm:ss"); qDebug() << dt.date(); return QDateTime::fromString(data.value(7), "yyyy-MM-dd hh:mm:ss"); }
-
QDateTime dt; dt.fromString("2019-06-16 09:45:05", "yyyy-MM-dd hh:mm:ss"); qDebug() << dt.date();
Try:
QDateTime dt = QDateTime::fromString("2019-06-16 09:45:05", "yyyy-MM-dd hh:mm:ss"); qDebug() << dt.date();
-
@aha_1980 answer is correct but a bit more explanation.
QDateTime::fromString
is static so i can't change the item you call it from, you have to assign something to it -
also, for your request the formatstring should be
dt = QDateTime::fromString("2019-06-14T08:43:48.000", "yyyy-MM-ddThh:mm:ss.zzz");
-
@Gerd Or, since that's the ISO-8601 representation:
QDateTime::fromString("2019-06-14T08:43:48.000", Qt::ISODateWithMs)
-
@Gerd it worked, but it's strange that the result of the query is with a 'T' in the timestamp while the server is not present
-
@Samuel-Ives said in QDateTime fromString () does not work:
but it's strange that the result of the query is with a 'T' in the timestamp while the server is not present
Do you mean you're not connected to the SQL server? In that case you can't execute queries, so I'm wondering what exactly you're doing.
-
@VRonin: good point...
@Samuel-Ives : what do you mean with "server is not present" ? There should be no query run against a non present server... -