abs failing for datetime timedelta

Errors and unexpected results
Post Reply
User avatar
joleenf
Posts: 1119
Joined: Mon Jan 19, 2009 7:16 pm

abs failing for datetime timedelta

Post by joleenf »

Hi,

I would expect that this piece of code would work okay in the jython shell, but it doesn't...

Code: Select all

from datetime import datetime as dt

dateTime1=dt.strptime('2017-Aug-21 18:50', '%Y-%b-%d %H:%M')
dateTime2=dt.strptime('2017-Aug-20 17:20', '%Y-%b-%d %H:%M')

print (abs(dateTime1 - dateTime2))


Thanks,
Joleen
User avatar
joleenf
Posts: 1119
Joined: Mon Jan 19, 2009 7:16 pm

Re: abs failing for datetime timedelta

Post by joleenf »

Sorry, it looks like for python 2.7, the code needs to be

from datetime import datetime as dt

dateTime1=dt.strptime('2017-Aug-21 18:50', '%Y-%b-%d %H:%M')
dateTime2=dt.strptime('2017-Aug-20 17:20', '%Y-%b-%d %H:%M')

print (abs((dateTime1 - dateTime2).total_seconds()))

Thanks,
Joleen
Post Reply