0

I need to save a timedelta value into a float type fields.

Here is my code:

if self.end_time and self.start_time:
        timediff =self.end_time - self.start_time // Here i get a time.delta value
        self.duration = // here i get an error

TypeError: float() argument must be a string or a number, not
'datetime.timedelta'

How can I resolve this?

Zero Piraeus
  • 56,143
  • 27
  • 150
  • 160
KbiR
  • 4,047
  • 6
  • 37
  • 103
  • The code above is not valid Python: `//` is not a comment marker in Python, and your `self.duration =` assignment is missing exactly the code fragment which is causing your problem. Please paste something that actually exhibits the behaviour you describe. – Zero Piraeus Feb 26 '19 at 13:59

1 Answers1

0

The code in your question is incomplete, but assuming you're actually doing something like this:

self.duration = float(timediff)

… and what you want is the total number of seconds represented by timediff as a float, you can get that using the timedelta.total_seconds() method:

self.duration = timediff.total_seconds()

This returns a float, so no conversion is necessary.

Zero Piraeus
  • 56,143
  • 27
  • 150
  • 160