Subtracting dates to get hours and minutes
Thomas Passin
list1 at tompassin.net
Tue Dec 13 23:19:58 EST 2022
Your problem is that datetime.datetime does not accept a tuple as an
argument. It expects an integer value for the first argument, but you
supplied a tuple. In Python, you can use a sequence (e.g., tuple or
list) the way you want by prefixing it with an asterisk. This causes
the sequence of items to be treated as individual arguments. So:
Startt = datetime.datetime(2022, 12, 13, 5, 3, 30)
st1 = (2022, 12, 13, 5, 3, 30)
dts1 = datetime.datetime(*st1) # NOT datetime.datetime(st1)
dts1 == Startt # True
On 12/13/2022 10:43 PM, Gronicus at SGA.Ninja wrote:
> As is, Test A works.
> Comment out Test A and uncomment Test B it fails.
> In Test B, I move the data into a variable resulting with the report:
> "TypeError: an integer is required (got type tuple)
>
> How do I fix this?
>
> #-----------------------------------------------------------------------------
> import datetime
> #=================================================
> # Test A Hard coded Date/Time
> Startt = datetime.datetime(2022, 12, 13, 5, 3, 30)
> Stopp = datetime.datetime(2022, 12, 12, 21, 15, 30)
>
> # =================================================
> # Test B Date/Time data as a variable
> #Startt = (2022, 12, 13, 5, 3, 30)
> #Stopp = (2022, 12, 12, 21, 15, 30)
>
> #Startt = datetime.datetime(Startt)
> #Stopp = datetime.datetime(Stopp)
>
> # =================================================
> c = Startt - Stopp
> minutes = c.total_seconds() / 60
> minutes = c.seconds / 60
> hours = 0
>
> while (minutes > 59):
> minutes = minutes - 60
> hours += 1
> minutes = round(minutes)
> print()
> print (" Hours = <" + str(hours) + ">")
> print (" Minutes = <" + str(minutes) + ">")
>
> # -----------------------------------------------------------------------------
>
More information about the Python-list
mailing list