I have a tuple that looks like this
(datetime.datetime(2015, 8, 25, 14, 8, 56),)
And I want to convert it to a datetime object, in python?
How can I do this?
I have tried
import datetime
time = datetime.datetime(my_tuple)
Bu that didn't work.
Just get the first element of the tuple, which is a datetime object already.
time = my_tuple[0]
I suspect the OP is asking how to unpack the tuple into arguments.
from datetime import datetime
#Typical datetime call
my_date1 = datetime(2022,1,28)
#If year,month, and day are in a tuple
my_tuple = (2022,1,28)
my_date2 = datetime(*my_tuple) #Unpacks the tuple into datetime arguments
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With