RajeevSahu
RajeevSahu

Reputation: 1

Conversion problem from one QDateTime format to another

I want to convert a QDateTime of format

Sat Feb 12 00:00:00 2011

to

d/m/yyyy h:mm:ss AP

i.e "12/02/2011 12:0:0 AM".

How could it be done?

Upvotes: 0

Views: 6567

Answers (2)

user362638
user362638

Reputation:

Fix your formatting string. You are using "mm" for month, but "mm" means minutes with leading zero. Use "MM" for month. Also, if you really want to present minutes and seconds without leading zeros as in your question, use only single "m" and "s":

QString str = tempDateTime.toString("dd/MM/yyyy h:m:s AP"));

Upvotes: 0

Johan
Johan

Reputation: 20763

Probably using the functions

  • QDateTime::fromString
  • QDateTime::toString

But try to use the ISO based timeformat if you can, this kind of odd formating with both weekday and monthday information can become quite funny if it is not a valid date.

The normal example could look like (using iso format):

QDateTime myTime;
qDebug() << myTime.fromString("2010-01-01 17:00", "yyyy'-'MM'-'dd' 'hh':'mm").toString("yyyy-MM-dd hh:mm:ss");

I tried to use your input format in the fromString, but the results was a little bit strange. So maybe you need to use some classic string manipulation to create a valid date string with some regexps before you can start to work with your date.

/Good luck

Upvotes: 2

Related Questions