开发者

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开发者_高级运维?


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


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"));
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜