简体   繁体   English

无法将 QString 转换为 QDateTime

[英]Can not convert QString to QDateTime

I have to convert QString to QDateTime by QDateTime::fromString method.我必须通过 QDateTime::fromString 方法将 QString 转换为 QDateTime。 I have QString object which contains "Wed, 13 Jun 2018 12:52".我有包含“Wed, 13 Jun 2018 12:52”的 QString 对象。 But when I use it QDateTime::fromString returns invalid object and I don't knew why.但是当我使用它时 QDateTime::fromString 返回无效的对象,我不知道为什么。 I use "ddd, MM-MMM-yyyy HH:MM" format.我使用“ddd,MM-MMM-yyyy HH:MM”格式。 Could anyone tell me what I doing wrong?谁能告诉我我做错了什么?

My code:我的代码:

QString tempDate; //Wed, 13 Jun 2018 12:52
QDateTime::fromString(tempDate, "ddd, MM-MMM-yyyy HH:MM"); //returns invalid obj

Your QDateTime format does not correspond to your input string.您的 QDateTime 格式与您的输入字符串不符。

Wed, 13 Jun 2018 12:52 should be matched with ddd, dd MMM yyyy HH:mm . Wed, 13 Jun 2018 12:52应与ddd, dd MMM yyyy HH:mm匹配。

See QDateTime::fromString .请参阅QDateTime::fromString

Also, make sure you're using the correct locale when doing the conversion, as ddd and MMM are localized.此外,请确保在进行转换时使用正确的语言环境,因为dddMMM已本地化。 Either change the local with QLocale::setDefault , or with QLocale::toDateTime :使用QLocale::setDefaultQLocale::toDateTime更改本地:

QLocale(QLocale::English).toDateTime(tempDate, "ddd, dd MMM yyyy HH:mm");

QDateTime startTime = QDateTime::fromString (QString("1970-07-18T00:00:00"), Qt::ISODate);

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM