0

I have a QDateTime which stores the time and the date in a fromat like this

>>> now = QtCore.QDateTime.currentDateTime()
>>> print(now)
>>> PyQt4.QtCore.QDateTime(2015, 8, 28, 17, 15, 33, 340)

And I have QDateTimeEdit which stores the date in the same format as above.

What I'm trying to do is convert the difference between the two DateTimes in seconds.

So 28.8.2015 17:20 (DD.M.YYYY HH:SS) and 28.8.2015 17:25 would be difference 5 minutes and that's 300 (seconds).

Is there a simple and pythonic way of doing this?

J. Doe
  • 77
  • 5
  • 11

1 Answers1

0

use the secsTo function

now = QtCore.QDateTime.currentDateTime()
#got the current time 
dt2 = QtCore.QDateTime.fromString("1-1-98 00:01:02","M-d-yy hh:mm:ss")
#got another date
t = now.secsTo (dt2)
#t is the difference between these two times in seconds
tms =now.msecsTo (dt2)
#tms is the difference between these two times in milliseconds

another way is to get the time stamps of both dates and find the difference between the time stamps

now = QtCore.QDateTime.currentDateTime()
#got the current time 
dt2 = QtCore.QDateTime.fromString("1-1-98 00:01:02","M-d-yy hh:mm:ss")
#got another date

t = now.toTime_t()- dt2.toTime_t()
#t is the difference between these two times in seconds
Midhun
  • 3,850
  • 1
  • 23
  • 26