Python Forum
Thread Rating:
  • 1 Vote(s) - 4 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Time conversion error
#1
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!
Reply
#2
I found the solution thwt fixes the error:
print(timedelta(seconds=td_sec))
thank you!
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  Plotting by Time, error mansoorahs 1 737 May-16-2023, 09:46 AM
Last Post: Larz60+
  Time Limit Exceeded error loves 5 3,160 Dec-03-2020, 07:15 AM
Last Post: Sofia_Grace
  Read CSV error: python KeyError: 'Time' charlicruz 1 5,179 Jun-27-2020, 09:56 AM
Last Post: charlicruz
  Data Type conversion error rajeevjagatap 2 4,354 Apr-15-2020, 03:29 PM
Last Post: rajeevjagatap
  Error when running the second time metro17 3 3,764 Aug-30-2019, 12:09 PM
Last Post: ThomasL
  python opencv grayscale conversion error Spandora 1 9,582 May-26-2019, 10:43 AM
Last Post: heiner55
  pyad time conversion error (plus solution) PapaZod 2 3,511 Nov-25-2018, 02:42 PM
Last Post: PapaZod
  Error during 2to3 conversion krow4869 3 3,988 Oct-14-2018, 08:19 AM
Last Post: Larz60+
  Conversion of Time Duration in seconds in python jdevansh99 0 2,863 Jun-05-2018, 05:12 PM
Last Post: jdevansh99
  Getting error in finding time.time() value in python Lakshana 1 2,501 Jan-11-2018, 07:07 AM
Last Post: wavic

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020