Subtracting dates to get hours and minutes

Thomas Passin list1 at tompassin.net
Wed Dec 14 09:30:54 EST 2022


On 12/14/2022 12:55 AM, Gronicus at SGA.Ninja wrote:
> 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....

It demonstrates that the version with the "*" gives the same result as 
the first expression.  That line is not needed by any code, it's just 
there to show you that the proposed expression gives the desired result.

> 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