QDateTime :: fromString返回无效的日期,我缺少什么?

use*_*081 3 c++ qt qdatetime

我有一些代码从sqlite数据库读取日期时间,datetime作为字符串返回.当我尝试使用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);
Run Code Online (Sandbox Code Playgroud)

Chr*_*n.K 10

没有QT的专家,但如果QDateTime::fromString()按照(合理地)期望的那样工作,并且根据这个,你没有使用正确的模式.

您指示从sqllite数据库读取的字符串类似于"2012-01-17 19:20:27.0",那么您的格式应该是这样的yyyy-MM-dd HH:mm:ss.z.

详细地:

  • 你的分隔符应该是' - '而不是'/'(如你在示例中所示)
  • 时间似乎是24小时格式(19 - > 7 pm)(因此使用HH而不是hh)
  • 你有一位数毫秒,所以添加.z.