Python Forum

Full Version: TypeError: unsupported operand type(s) for *: 'datetime.timedelta' and 'float'
You're currently viewing a stripped down version of our content. View the full version with proper formatting.
Hi all,

I am working on datetime.datetime objects to calculating between 19:00 and 22:15. I want to calculating the datetime objects and then multiply by the value 11.4.

When I try this:

program_time = program_stop_time - program_start_time
program_duration = program_time * 11.4
I will get an error: TypeError: unsupported operand type(s) for *: 'datetime.timedelta' and 'float'


Here it is the input for the variables:

program_start_time:

2018-05-01 19:00:00
program_stop_time:

2018-05-01 22:15:00
Here is the return output when I calculating:

3:15:00
Here is the code:

start_time = time.strptime(program_start_date, '%Y%m%d%H%M%S')
 program_start_time = datetime.datetime.fromtimestamp(time.mktime(start_time))
stop_time = time.strptime(program_stop_date, '%Y%m%d%H%M%S')
program_stop_time = datetime.datetime.fromtimestamp(time.mktime(stop_time))

program_duration = program_stop_time - program_start_time
Here is what I want to achieve:

3591
Can you please help me how to correct the error to allow me to calculating the datetime object and multiply the value to get the return output I want??
something like this?
>>> import datetime
>>> import time
>>> start_time = datetime.datetime.now()
>>> end_time = datetime.datetime.now()
>>> start_time
datetime.datetime(2018, 5, 1, 15, 15, 17, 872088)
>>> end_time
datetime.datetime(2018, 5, 1, 15, 15, 25, 640901)
>>> elapsed_time = end_time - start_time
>>> ts = elapsed_time.total_seconds() * 11.4
>>> ts
88.5644682
>>>
(May-01-2018, 07:08 PM)chris0147 Wrote: [ -> ]Here is what I want to achieve:
3591

But what is that? Hours? Minutes? Seconds?
It's an unsupported operation, because multiplying a complex timespan by 11.4 doesn't make sense.