PDA

View Full Version : How to get the custom date and time format???



Gokulnathvc
29th July 2011, 14:20
How to get the custom date and time format???
2011-07-29 18:55:09

How to format the date and time to this format??

FelixB
29th July 2011, 14:23
the class QDateTime provides everything you need.

Gokulnathvc
29th July 2011, 14:41
QString dateTimeString = dateTime.toString();

QDateTime dateTimenew(QDateTime::fromString(dateTimeString, "yyyy-MM-dd hh:mm:ss"));

I have tried using the above code, but it fails..

FelixB
29th July 2011, 14:59
what exactly do you want? create a time format to a string? or convert a string to time format?

Gokulnathvc
29th July 2011, 15:01
Could you please explain with my code above??

FelixB
29th July 2011, 15:05
ok, I'll try.

you convert a QDateTime (at least I hope it's a QDateTime) into a QString. You use Qt::TextDate as string format. Then you reconvert this string into a QDateTime using a different format ("yyyy-MM-dd hh:mm:ss"). No wonder that fails.

CroOm
29th July 2011, 16:29
try this one:

QDateTime dateTime = dateTime.currentDateTime();
QString dateTimeString = dateTime.toString("yyyy-MM-dd hh:mm:ss");

ChrisW67
29th July 2011, 22:47
Could you please explain with my code above??

Can you please, just for once, answer the question put to you. Which do you want; to convert a date/time object to a string, or to convert a string to a date/time object?

As explained by FelixB your code is doomed to fail because the first line produces a string that the second line does not match. If it did what you seem to intend then this is much faster:


QDateTime dateTimenew = dateTime;

and there is no string involved.