'Tuple to datetime object
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.
Solution 1:[1]
Just get the first element of the tuple, which is a datetime object already.
time = my_tuple[0]
Solution 2:[2]
see link
https://www.saltycrane.com/blog/2008/11/python-datetime-time-conversions/
from datetime import datetime
start_date = datetime(2019,8,2)
expiry_dates = datetime(2019,10,2)
Solution 3:[3]
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
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|---|
| Solution 1 | TigerhawkT3 |
| Solution 2 | Akber Iqbal |
| Solution 3 | LazyCoder1982 |
