Unsolved Why this date.fromstring is not working ?
-
Hi,
Below code gives invalid Date, what am I missing ?
DATE.fromString("17-19-47 2019-12-10" , "hh-mm-ss yyyy-MM-dd")
-
@RahibeMeryem fromString is static, no need for DATE:
QDate::fromString("17-19-47 2019-12-10" , "hh-mm-ss yyyy-MM-dd");
-
@RahibeMeryem in addition to to @jsulm
as a static function, fromString returns a new Date and does not manipulate the QDate where you call it from.
So, do you assign it? aka:
DATE = DATE.fromString("17-19-47 2019-12-10" , "hh-mm-ss yyyy-MM-dd")
-
Hi,
In addition to my fellows:
DATE = QDate::fromString("2019-12-10" , "yyyy-MM-dd");
is the conventional way to call a static function. It shows clearly that you are using a static function and want to get back the value it returns.
[edit: Removed time related formatting as it's not related to QDate SGaist]
-
@SGaist said in Why this date.fromstring is not working ?:
DATE = QDate::fromString("17-19-47 2019-12-10" , "hh-mm-ss yyyy-MM-dd");
QDate DATE2 = QDate::fromString("17-19-47 2019-12-10" , "hh-mm-ss yyyy-MM-dd");
Still gives Invalid error DATE2 Inalid
.. What am I missing ?
-
@RahibeMeryem Change hh to HH
-
@RahibeMeryem
I don't know whether what @jsulm has just written is correct or not, but https://doc.qt.io/qt-5/qdate.html#fromString-1 says it only accepts date format specifiers, you have date and time. Don't you needQDateTime::fromString()
, as in https://doc.qt.io/qt-5/qdatetime.html#fromString-1 ? Then you may want theHH
instead ofhh
(read the doc page for the distinction). -
@JonB said in Why this date.fromstring is not working ?:
what @jsulm has just written is correct or not
It is - just tested.
-
@jsulm
You tested it withQDate
instead ofQDateTime
, when the OP is passing a date+time value and date+time format? He wants to pass in a date+time with all the formatting for time too, yet he only wants a date returned? -
@JonB @RahibeMeryem Yes, with QDateTime it works with hh also.
Sometimes it's so easy :-) -
@jsulm
My point being: if you really only want aQDate
back, why would you include the time specifiers in the format string? I'm wondering whether the user intendsQDateTime::fromString()
...? -
@JonB said in Why this date.fromstring is not working ?:
My point being: if you really only want a QDate back, why would you include the time specifiers in the format string?
You should ask OP, not me
-
@jsulm
My use of "you" is the "communal" "you", not the "personal" "you" aimed at you :) -
@jsulm , @RahibeMeryem
I'm sorry, but it is not theHH
vshh
(don't know what you tested for that @jsulm), it is indeed as I said that you needQDateTime
for your format:>>> from PySide2 import QtCore >>> QtCore.QDate.fromString("17-19-47 2019-12-10" , "hh-mm-ss yyyy-MM-dd") PySide2.QtCore.QDate(0, 0, 0) >>> QtCore.QDateTime.fromString("17-19-47 2019-12-10" , "hh-mm-ss yyyy-MM-dd") PySide2.QtCore.QDateTime(2019, 12, 10, 17, 19, 47, 0, 0) >>> QtCore.QDate.fromString("17-19-47 2019-12-10" , "HH-mm-ss yyyy-MM-dd") PySide2.QtCore.QDate(0, 0, 0) >>> QtCore.QDateTime.fromString("17-19-47 2019-12-10" , "HH-mm-ss yyyy-MM-dd") PySide2.QtCore.QDateTime(2019, 12, 10, 17, 19, 47, 0, 0) >>>
The reason being: if you use
QDate
, the leading"HH-mm-ss"
or whatever will not match against the time, instead it will be treated as literal and therefore the input will fail to match, whatever you do.... -
@JonB That's why I wrote "Yes, with QDateTime it works with hh also.".
With QDate it works with HH, but not hh - I tested that:QDate DATE2 = QDate::fromString("17-19-47 2019-12-10" , "HH-mm-ss yyyy-MM-dd");
And yes, it is correct that one should use QDateTime instead of QDate if there is time and date.
-
@jsulm
If it works for you withHH
plusQDate
, could you please explain the output I showed above where it does not work? Thanks.>>> QtCore.QDate.fromString("17-19-47 2019-12-10" , "HH-mm-ss yyyy-MM-dd") PySide2.QtCore.QDate(0, 0, 0)
which is an invalid
QDate
. I am Qt 5.12.2, Ubuntu 19.04. -
From my point of view, your both wrong @JonB & @jsulm ๐
the docu only states https://doc.qt.io/qt-5/qdate.html#fromString-1
that only
y
andM
andd
are valid formats everything else is interpreted as a literal character.and my test confirms it:
int main(int argc, char *argv[]) { QApplication a(argc, argv); QDate d1 = QDate::fromString("17-19-47 2019-12-10" , "hh-mm-ss yyyy-MM-dd"); QDate d2 = QDate::fromString("17-19-47 2019-12-10" , "HH-mm-ss yyyy-MM-dd"); QDate d3 = QDate::fromString("17-19-47 2019-12-10" , "17-19-47 yyyy-MM-dd"); QDate d4 = QDateTime::fromString("17-19-47 2019-12-10" , "hh-mm-ss yyyy-MM-dd").date(); qDebug() << d1 << d2 << d3 << d4; // return a.exec(); } Result: QDate(Invalid) QDate(Invalid) QDate("2019-12-10") QDate("2019-12-10")
-
@JonB OK, correction: with QDate it does not work (not with hh and not with HH). I was thinking it does, because my small test application outputs date/time in another place, so it was looking like it was working. Sorry for confusion :-)
-
@J-Hilk said in Why this date.fromstring is not working ?:
Interesting, because what you have shown is exactly what I have said! :)
In the case of working
QDate d3 = QDate::fromString("17-19-47 2019-12-10" , "17-19-47 yyyy-MM-dd");
that works only because the literal leading
17-19-47
in the format string matches the input without "placeholder substitutions".As for the
HH
vshh
when usingQDateTime
, that should be irrelevant for the input shown, as per https://doc.qt.io/qt-5/qdatetime.html#fromString-1hh
the hour with a leading zero (00 to 23 or 01 to 12 if AM/PM display)
HH
the hour with a leading zero (00 to 23, even with AM/PM display)as the only difference is to do with "AM/PM", which the input string does not have.
-
@JonB ๐sorry I got the impression you were arguing for the lower case
hh
which is of course wrong in Date::fromString() missed the other post!