PDA

View Full Version : Converting a QString to QDate



Stanfillirenfro
31st March 2014, 12:44
Hi!
I am struggling right now with the conversion of a string to a date, but I am not succeeding. Could someone help?

My string is in the following format:
Sa Feb 22 2014.

After compiling, qDebug prints an empty QDate (
QDate(""))

I have the following code:


QString strDate1, strDate2;
QDate strDate;

strDate1 = Sa Feb 22 2014;
strDate2 = QString::fromStdString(strDate1.toStdString());
strDate.fromString(strDate2, "dd mmm ww yyyy");

Many thanks in advance

anda_skoa
31st March 2014, 14:07
Line 4 of your code is missing quotes.

Line 5 is a needless two way conversion, strDate1 is already a QString

Line 6 calls a static method (fromString) like a member method. The call's return value is not used.
"dd" means a day number, "mmm" does not exist in the date context, "ww" does not exist in date formats.

Cheers,
_

Santosh Reddy
31st March 2014, 14:13
This is a typical way to do it.


QString str = "Sat Feb 22 2014";
QDate strDate;

strDate = QDate::fromString(str, "ddd MMM dd yyyy");

qDebug() << strDate;

Stanfillirenfro
31st March 2014, 14:21
Hi Reddy!
You are just an Angel!!!! Your code has solved the problem. It is exactly what I wanted to have.

Many thanks for your help.