4

SQLite データベースから日時を読み取るコードがいくつかあります。日時は文字列として返されます。QDateTime::FromString を使用して日付に変換しようとすると、無効な日付が返されます。以下は、データベースと変換から返された時間です。これが解析できないのはなぜですか?

// -this is the value returned from the DB currentServerTime=2012-01-17 19:20:27.0

QString format("yyyy/MM/dd hh:mm:ss");
QString qCurrentServerTime(currentServerTime);
now = QDateTime::fromString(qCurrentServerTime, format);
4

1 に答える 1

10

No expert in QT, but if QDateTime::fromString() works as one would (reasonably) expect and according to this, you're not using the correct pattern.

You indicate the string read from the sqllite database is like "2012-01-17 19:20:27.0", then your format should be like yyyy-MM-dd HH:mm:ss.z.

In detail:

  • Your separator should by '-' not '/' (as you show in the example)
  • The time seems to be in 24 hours format (19 -> 7 p.m.) (so use HH instead of hh)
  • You have one digit for milliseconds, so add .z.
于 2012-01-19T14:46:13.463 に答える