Universal way to parse QString with non-fixed date-time formats for QDateTime conversion
-
Hi
I need to parse a
QString
and convert it toQDateTime
. My Problem is that the string is in users locale format, e.g. "24-4-2021 16:17:18" or "4/24/2021 16:17:18" or "24.04.2021 16:17:18". Depending on users locale even text format is possible like "Apr 24, 2021 4:17:18 pm".Microsofts MFC has a function
COleDateTime::ParseDateTime()
that was able to handle different formats. Is their something similar in Qt or does anybody know about an open source solution? -
Hi,
Isn't QLocale::toDateTime what you are looking for ?
-
Thanks for the answer.
I am afraid
QLocale::toDateTime
still depends of the current QLocale setting. e.g. aQLocale("de_DE")
cannot parse "3:45:21 pm" as german time has no concept of am/pm.May be I can check several locales and can cover the most frequent time presentations used by the customers. I'll take a look at it ...
-
@Qnut said in Universal way to parse QString with non-fixed date-time formats for QDateTime conversion:
COleDateTime::ParseDateTime
Checking the documentation of that method, it does not seem to be as universal as you describe it. It also uses the system locale by default.