Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Tuple to datetime object

Tags:

python

time

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.

like image 652
spen123 Avatar asked Aug 24 '26 02:08

spen123


2 Answers

Just get the first element of the tuple, which is a datetime object already.

time = my_tuple[0]
like image 111
TigerhawkT3 Avatar answered Aug 25 '26 16:08

TigerhawkT3


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
like image 42
LazyCoder1982 Avatar answered Aug 25 '26 15:08

LazyCoder1982