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

QDateTime::fromString returns invalid Date, what am I missing?

本文关键字:什么 日期 fromString 返回 无效 QDateTime      更新时间:2023-10-16

我有一些代码从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);

QT方面没有专家,但如果QDateTime::fromString()如人们(合理地)预期的那样工作,并且根据这一点,您没有使用正确的模式。

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

详细信息:

  • 您的分隔符应该是'-',而不是'/'(如示例所示)
  • 时间似乎是24小时格式(19->晚上7点)(因此使用HH而不是hh
  • 您有一个数字表示毫秒,所以添加.z