PDA

View Full Version : Qstring to Qdate and vice versa



sai_3289
11th February 2013, 11:41
How to convert Qstring to date??? and again date to Qstring....? Means i want Qstring of type dd-MMM-yyyy to yyyy-MM-dd...?
Qstring str="11-Feb-2013" Now i wants the string in "2013-02-11"..?
What i tried is
QDate da=QDate::fromString(str,"dd-MMM-yyyy");
qDebug()<<"FINALISED DATE____________##########"<<da.toString("yyyy-MM-dd");

---------------Output---------
FINALISED DATE____________########## ""
its printing empty string please correct me...?

wysota
11th February 2013, 11:43
What does qDebug() << da print?

airproject
11th February 2013, 12:07
Hi,
First check if the date was assigned at all with some predefiend Qt format e.g. QDate::toString(Qt::DefaultLocaleShortDate). If this will be empty it means that the date is invalid and "fromString" failed.

sai_3289
11th February 2013, 12:22
thanks for addressing wysota ............it prints QDate("") ...? WHy is that so....wats wrong in my first statement...?

airproject thanks for reply.....I didnt get u .????

Lesiok
11th February 2013, 12:53
What is Yours locale ? if it is not some English the name of the month will not be recognized.
What You have from this :
qDebug << QDate(2013,2,11).toString("dd-MMM-yyyy);

sai_3289
11th February 2013, 13:05
thanks for addressing lesiok ...the o/p i am getting is "11-Feb-2013"

airproject
11th February 2013, 13:43
Hi, i meant to check if the date is valid first:
Qstring str="11-Feb-2013";
QDate da=QDate::fromString(str,"dd-MMM-yyyy");
if(da.isValid())
qDebug()<<"FINALISED DATE____________##########"<<da.toString(Qt:DefaultLocaleShortDate);

regards.

Lesiok
11th February 2013, 14:08
thanks for addressing lesiok ...the o/p i am getting is "11-Feb-2013"
So it should working :confused:

sai_3289
12th February 2013, 05:29
Hi,
@lesiok :but its not working ....i am not getting wat exactly the problem is ..?
@airproject :when i print directly "da" it prints QDate("") ...?so it wont go to if loop at all...?

Guys Please me as it is very urgent or guide me any other through which i can slove my problem ....?


Regards
Beginner

Added after 26 minutes:

Hi,

Sorry Guys...problem was solved............. i have given like this QDate da=QDate::fromString(str,"dd-MMM-yyy") where i am missing one 'Y' in this.....

Lesiok
12th February 2013, 06:54
Next time : don't retype the code but use "copy & paste" :)

sai_3289
12th February 2013, 11:54
Hi,

Yeah sure Lesiok.....Thankzzzzz