Python Forum
Time conversion error - Printable Version

+- Python Forum (https://python-forum.io)
+-- Forum: Python Coding (https://python-forum.io/forum-7.html)
+--- Forum: General Coding Help (https://python-forum.io/forum-8.html)
+--- Thread: Time conversion error (/thread-30629.html)



Time conversion error - tester_V - Oct-28-2020

Hi, I'm trying to convert "time" string and a code I wrote works fine until I add a print statement.
It produces error " print(datetime.timedelta(td_sec))
AttributeError: type object 'datetime.datetime' has no attribute 'timedelta'"
Code:
from datetime import datetime
# Some code here #
    if tstamp1 > tstamp2:   
        td = tstamp1 - tstamp2                                    
        tdif = str(td)
        print ("TIME Difference -------------------------------",tdif)
        td_sec = int(round(td.total_seconds()))
        td_sec=int(td_sec)
        print('The difference IN SEONDS  ---------%s seconds' % td_sec)
Print statement That produses the error:

print(datetime.timedelta(td_sec))
Thank you! Any help will be appreciated!


RE: Time conversion error - tester_V - Oct-28-2020

I found the solution thwt fixes the error:
print(timedelta(seconds=td_sec))
thank you!