I now, the question sounds rather silly, but I just can't get it to work. Worst example:
QString time_format = "yyyy-MM-dd HH:mm:ss";
QDateTime a = QDateTime::currentDateTime();
QString as = a.toString(time_format);
qDebug() << as; // print "2014-07-16 17:47:04"
QDateTime b;
b.fromString(as,time_format);
assert(b.isValid()); // fails
I create a valid QDatetime, make a string out of it (that is correct) and try to turn it into a QDatetime again (using the same time_format-string). But suddenly, the string can't be parsed.
Any ideas?
You want this->getBookingDate(). toString("yyyy. MM. dd") .
QDateTime::QDateTime(QDate date, QTime time, Qt::TimeSpec spec = Qt::LocalTime, int offsetSeconds = 0) Constructs a datetime with the given date and time, using the time specification defined by spec and offsetSeconds seconds. If date is valid and time is not, the time will be set to midnight.
Qt5 current date & timeQDate cd = QDate::currentDate(); The QDate::currentDate static function returns the current date. QTime ct = QTime::currentTime();
Returns the datetime as the number of seconds that have passed since 1970-01-01T00:00:00, > Coordinated Universal Time (Qt::UTC). On systems that do not support time zones, this function will behave as if local time were Qt::UTC. See also setTime_t(). just tried pass string returned by QDateTime::currentDateTime().
fromString
is a static function that returns the date; so you need to do:
QDateTime b = QDateTime::fromString(as,time_format);
in your code b
never chaged from its default initialized state
QString as = QDateTime::currentDateTime().toString("yyyy-MM-dd HH:mm:ss");
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With