Subtracting dates to get hours and minutes

Gronicus at SGA.Ninja Gronicus at SGA.Ninja
Wed Dec 14 00:55:49 EST 2022


I realized it had something to do with tupilation
The simple fix is to add the * into the original code.
Startt = datetime.datetime(*Startt)

I am not sure what "dts1 == Startt  # True" does....

Thank you.....


-----Original Message-----
From: Python-list <python-list-bounces+gronicus=sga.ninja at python.org> On
Behalf Of Thomas Passin
Sent: Tuesday, December 13, 2022 11:20 PM
To: python-list at python.org
Subject: Re: Subtracting dates to get hours and minutes

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) + ">")
> 
> # 
> ----------------------------------------------------------------------
> -------
> 

--
https://mail.python.org/mailman/listinfo/python-list



More information about the Python-list mailing list