Question

I'm having trouble with a seemingly very simple problem: I want to get a QDateTime from a QString containing a timestamp. I got the timestamp from PostgreSQL, but it doesn't matter. Here is the code that does not work:

QString timestamp = "2010-10-09 19:21:46+02:00";
QString format = "YYYY-MM-DD HH:MM:SSTZD";
QDateTime dt = QDateTime::fromString(timestamp, format);
qDebug() << dt.toString(); // outputs empty string

There must be something very obvious I'm missing. Thanks!

Was it helpful?

Solution

There were two mistakes I was making. There is no TZD in the format specifications, so I removed the time zone information since I do not need it in my app by doing:

timeStamp.chop(6);

And then used the following format to get a QDateTime. Note the lowercase format characters:

QDateTime createdAt = QDateTime::fromString(timeStamp, "yyyy-MM-dd HH:mm:ss");

Thanks to everyone above for helping out.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top