Creating QDateTime from string
-
wrote on 23 Aug 2021, 10:28 last edited by SPlatten
I am trying to create an instance of QDateTime from a string, the string I have is in the format:
yyyy/MM/dd hh:mm:ss.zzz
I've tried:
QDateTime dtmCreated(QDateTime::fromString(strValue));
Where strValue contains:
2021-08-20T06:45:18
I then output dtmCreated to the console:
qDebug() << strValue << dtmCreated.isValid();
isValid returns false, I've also tried:
QDateTime dtmCreated(QDateTime::fromString(strValue, "yyyy/MM/ddThh:mm:ss"));
Same result isValid is false.
Also tried:
QDateTime dtmCreated(QDateTime::fromString(strValue, "yyyy-MM-dd HH:mm:ss"));
Same result isValid is false.
-
int main(int argc, char *argv[]) { QDateTime td = QDateTime::fromString("2021-08-20T06:45:18", "yyyy-MM-ddThh:mm:ss"); qDebug() << td.isValid() << td; return 0; }
-
I am trying to create an instance of QDateTime from a string, the string I have is in the format:
yyyy/MM/dd hh:mm:ss.zzz
I've tried:
QDateTime dtmCreated(QDateTime::fromString(strValue));
Where strValue contains:
2021-08-20T06:45:18
I then output dtmCreated to the console:
qDebug() << strValue << dtmCreated.isValid();
isValid returns false, I've also tried:
QDateTime dtmCreated(QDateTime::fromString(strValue, "yyyy/MM/ddThh:mm:ss"));
Same result isValid is false.
Also tried:
QDateTime dtmCreated(QDateTime::fromString(strValue, "yyyy-MM-dd HH:mm:ss"));
Same result isValid is false.
@SPlatten said in Creating QDateTime from string:
So, your date string contains-
as separators and your format is passed as/
2021-08-20T06:45:18
Do you see the discrepancy? 😉
-
@SPlatten said in Creating QDateTime from string:
So, your date string contains-
as separators and your format is passed as/
2021-08-20T06:45:18
Do you see the discrepancy? 😉
wrote on 23 Aug 2021, 10:36 last edited by@J-Hilk , edited post having tried something else, still not working.
-
int main(int argc, char *argv[]) { QDateTime td = QDateTime::fromString("2021-08-20T06:45:18", "yyyy-MM-ddThh:mm:ss"); qDebug() << td.isValid() << td; return 0; }
-
int main(int argc, char *argv[]) { QDateTime td = QDateTime::fromString("2021-08-20T06:45:18", "yyyy-MM-ddThh:mm:ss"); qDebug() << td.isValid() << td; return 0; }
wrote on 23 Aug 2021, 10:39 last edited by@J-Hilk Using "yyyy-MM-ddTHH:mm:ss" works, something minor, in the database using a length of 6 I store the date/timestamp to microsecond resolution, when converting to QDateTime, its only accurate to seconds.
-
@J-Hilk Using "yyyy-MM-ddTHH:mm:ss" works, something minor, in the database using a length of 6 I store the date/timestamp to microsecond resolution, when converting to QDateTime, its only accurate to seconds.
@SPlatten QDateTime should be able to handle milliseconds, because thats what QTime can handle, but nothing further.
-
@J-Hilk Using "yyyy-MM-ddTHH:mm:ss" works, something minor, in the database using a length of 6 I store the date/timestamp to microsecond resolution, when converting to QDateTime, its only accurate to seconds.
wrote on 23 Aug 2021, 11:26 last edited by@SPlatten said in Creating QDateTime from string:
when converting to QDateTime, its only accurate to seconds.
QDateTime
accuracy is down to milliseconds:// Read milliseconds time stamp QDateTime td = QDateTime::fromString("2021-08-20T06:45:18.234", "yyyy-MM-ddThh:mm:ss.zzz"); // add milliseconds to QDateTime dt = dt.addMSecs(998);
-
@SPlatten said in Creating QDateTime from string:
when converting to QDateTime, its only accurate to seconds.
QDateTime
accuracy is down to milliseconds:// Read milliseconds time stamp QDateTime td = QDateTime::fromString("2021-08-20T06:45:18.234", "yyyy-MM-ddThh:mm:ss.zzz"); // add milliseconds to QDateTime dt = dt.addMSecs(998);
wrote on 23 Aug 2021, 11:40 last edited by@KroMignon , it seems that although the database field is accurate to microseconds, when I read the data into the application the QSqlField:
QString strName(crField.name()), strValue(crField.value().toString());
crField is a constant reference passed in parameter of QSqlField. strValue when converting the TIMESTAMP(6) from the database which in the database contains 2021-08-20 06:46:18.832407 gets converted to 2021-08-20T06:46:18, this doesn't even contain milliseconds.
-
@KroMignon , it seems that although the database field is accurate to microseconds, when I read the data into the application the QSqlField:
QString strName(crField.name()), strValue(crField.value().toString());
crField is a constant reference passed in parameter of QSqlField. strValue when converting the TIMESTAMP(6) from the database which in the database contains 2021-08-20 06:46:18.832407 gets converted to 2021-08-20T06:46:18, this doesn't even contain milliseconds.
wrote on 23 Aug 2021, 12:04 last edited by KroMignon@SPlatten said in Creating QDateTime from string:
when converting the TIMESTAMP(6) from the database
According to
QVariant
documentation,QVariant::toDateTime()
will useQt::ISODate
and notQt::ISODateWithMs
. So with the default conversion, you will never got milliseconds.Do you really need micro-seconds in DB? Is
TIMESTAMP(3)
(milliseconds) not enough for you?
With this type, you should be able to parse the DATETIME with milliseconds (withQt::ISODateWithMs
/yyyy-MM-ddThh:mm:ss.zzz
). -
@SPlatten said in Creating QDateTime from string:
when converting the TIMESTAMP(6) from the database
According to
QVariant
documentation,QVariant::toDateTime()
will useQt::ISODate
and notQt::ISODateWithMs
. So with the default conversion, you will never got milliseconds.Do you really need micro-seconds in DB? Is
TIMESTAMP(3)
(milliseconds) not enough for you?
With this type, you should be able to parse the DATETIME with milliseconds (withQt::ISODateWithMs
/yyyy-MM-ddThh:mm:ss.zzz
).wrote on 23 Aug 2021, 12:09 last edited by@KroMignon , to be honest milliseconds is probably good enough, but the higher resolution the better in the database, from your post is there a way to get milliseconds is the conversion doesn't use Qt::ISODateWithMS ?
-
@KroMignon , to be honest milliseconds is probably good enough, but the higher resolution the better in the database, from your post is there a way to get milliseconds is the conversion doesn't use Qt::ISODateWithMS ?
wrote on 23 Aug 2021, 12:18 last edited by@SPlatten said in Creating QDateTime from string:
to be honest milliseconds is probably good enough, but the higher resolution the better in the database, from your post is there a way to get milliseconds is the conversion doesn't use Qt::ISODateWithMS ?
Yes, I think so:
QDateTime dt = QDateTime::fromString(crField.value().toString(), Qt::ISODateWithMS);
-
@SPlatten said in Creating QDateTime from string:
to be honest milliseconds is probably good enough, but the higher resolution the better in the database, from your post is there a way to get milliseconds is the conversion doesn't use Qt::ISODateWithMS ?
Yes, I think so:
QDateTime dt = QDateTime::fromString(crField.value().toString(), Qt::ISODateWithMS);
wrote on 23 Aug 2021, 12:23 last edited by@KroMignon, I'm not sure that is going to work because the toString function already removes the fraction part of the time.
1/12