[Qt-interest] How calculate time span (days, hours, minutes) QDateTime difference?

Dan White ygor at comcast.net
Fri Jun 11 22:52:32 CEST 2010


OK, how about this:

#include <QtCore/QCoreApplication>
#include <QTime>
#include <QDateTime>
#include <QDebug>

int main(int argc, char *argv[])
{
     QCoreApplication a(argc, argv);

     // DateTime departure = new DateTime(2010, 6, 12, 18, 32, 0);
     // DateTime arrival = new DateTime(2010, 6, 13, 22, 47, 0);
     // TimeSpan travelTime = arrival - departure;
     // Console.WriteLine("{0} - {1} = {2}", arrival, departure,  
travelTime);
     // The example displays the following output:
     //       6/13/2010 10:47:00 PM - 6/12/2010 6:32:00 PM = 1.04:15:00

     QDateTime departure = QDateTime::fromString ( "2010, 06, 12, 18,  
32, 00", "yyyy, MM, dd, HH, mm, ss" ) ;
     QDateTime arrival   = QDateTime::fromString ( "2010, 06, 13, 22,  
47, 00", "yyyy, MM, dd, HH, mm, ss" ) ;

     int days = departure.daysTo ( arrival ) ;

     QTime when = QTime ( 0, 0, 0, 0 ) ;

     when = when.addSecs ( departure.addDays(days).secsTo( arrival ) ) ;

     qDebug()
     << arrival.toString( "M/d/yyyy h:mm:ss AP" )
     << " - "
     << departure.toString( "M/d/yyyy h:mm:ss AP" )
     << " = "
     << days << when.toString( ".HH:mm:ss" ) ;
}

When I run it, I get:

"6/13/2010 10:47:00 PM"  -  "6/12/2010 6:32:00 PM"  =  1 ".04:15:00"

Now, can anyone tell me how to get rid of them quote marks ?

Why make another class ?  Seems like a waste.

On Jun 11, 2010, at 11:58 AM, Ed Sutton wrote:

>
>> http://doc.trolltech.com/4.6/qdatetime.html#secsTo
>
>> gives you seconds.
>
>> The rest is trivial.
>
> Thank you Dan and Jason.
>
> QDateTime::secsTo() does the trick with some additional work.
>
> I was hoping I had missed the Qt equivalent of the .NET TimeSpan  
> structure.
>
> http://msdn.microsoft.com/en-us/library/system.timespan.aspx
>
>
> DateTime departure = new DateTime(2010, 6, 12, 18, 32, 0);
> DateTime arrival = new DateTime(2010, 6, 13, 22, 47, 0);
> TimeSpan travelTime = arrival - departure;
> Console.WriteLine("{0} - {1} = {2}", arrival, departure, travelTime);
> // The example displays the following output:
> //       6/13/2010 10:47:00 PM - 6/12/2010 6:32:00 PM = 1.04:15:00
>
>
> Thanks again,
>
> -Ed
>
>
>
> “Sometimes I think the surest sign that intelligent life exists  
> elsewhere in the universe is that none of it has tried to contact us.”
> Bill Waterson (Calvin & Hobbes)
>
> ----- Ed Sutton <ESutton at fescorp.com<mailto:ESutton at fescorp.com>>  
> wrote:
> What is the Qt way to calculate the elapsed date time between two  
> QDateTime values?
>
> My goal is to display the difference in days, hours, and minutes.
>
> Thanks in advance,
>
> -Ed
>
>
> _______________________________________________
> Qt-interest mailing list
> Qt-interest at trolltech.com<mailto:Qt-interest at trolltech.com>
> http://lists.trolltech.com/mailman/listinfo/qt-interest
>
>
> _______________________________________________
> Qt-interest mailing list
> Qt-interest at trolltech.com<mailto:Qt-interest at trolltech.com>
> http://lists.trolltech.com/mailman/listinfo/qt-interest
>
>
> -Ed
>
>
> _______________________________________________
> Qt-interest mailing list
> Qt-interest at trolltech.com
> http://lists.trolltech.com/mailman/listinfo/qt-interest





More information about the Qt-interest-old mailing list