PDA

View Full Version : Date and Time format.



kaushal_gaurav
12th August 2008, 10:36
Hi,

I need to display the date and time in a particular format. I have a QDateTime Object.
How would i use QDateTime::fromString function to get the desired output.

i have the value of "Tue Aug 12 15:04:11 2008" in my QDateTime object.

I need to display it as "Today 15:04"

How would i do it.

Help!

Regards
GK

stefan
12th August 2008, 11:00
I think this is what you are looking for:

QString _OldDateTimeString = "Tue Aug 12 15:04:11 2008";
QString _OldFormat = "ddd mmm dd hh:mm:ss yyyy";
QDateTime _DateTime = QDateTime::fromString(_OldDateString, _OldFormat);
QString _NewDateTimeString=_DateTime::toString("ddd");
if(_DateTime.Date()==QDate::currentDate())
{
_NewDateTimeString="Today";
}
NewDateTimeString+=" "+_DateTime::toString("hh:mm");

kaushal_gaurav
12th August 2008, 11:12
Thanks...
but what if also i want to display "Yesterday" instead of yesterday's date.
Just as we see in MAC.

Does date functions support arithmatic (+, -)

stefan
12th August 2008, 11:36
Use daysTo function:

QDate _Today=QDate::currentDate();
QDate _SomeDate(2008,8,12);
QString _Day;
qint32 _Difference = _Today.daysTo(_SomeDate)
switch(_Difference)
{
case -1:
_Day="Yesterday";
break;
case 0:
_Day="Today";
break;
case 1:
_Day="Tomorrow";
break;
default:
_Day=_SomeDate.toString("dd mmm");
break
}