Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to use QDateTime::fromString?

Tags:

qt

qdatetime

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?

like image 671
FooTheBar Avatar asked Jul 16 '14 15:07

FooTheBar


People also ask

How do I convert QDateTime to Qstring?

You want this->getBookingDate(). toString("yyyy. MM. dd") .

How to set date and time in Qt?

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.

How do I get the current date and time in Qt C++?

Qt5 current date & timeQDate cd = QDate::currentDate(); The QDate::currentDate static function returns the current date. QTime ct = QTime::currentTime();

How do I get system time in Qt?

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().


2 Answers

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

like image 197
ratchet freak Avatar answered Sep 21 '22 19:09

ratchet freak


QString as = QDateTime::currentDateTime().toString("yyyy-MM-dd HH:mm:ss");
like image 29
Rajkumar Avatar answered Sep 17 '22 19:09

Rajkumar