How do you convert a python time.struct_time
object into a datetime.datetime
object? I have a library that provides the first one and a second library that wants the second one...
views:
1563answers:
2
+8
A:
Like this:
>>> structTime = time.localtime()
>>> datetime.datetime(*structTime[:6])
datetime.datetime(2009, 11, 8, 20, 32, 35)
Nadia Alramli
2009-11-08 20:30:37
Don't forget to #import time, datetime
jhwist
2009-11-08 21:00:40
@jhwist - some things people can be trusted to figure it out on their own :)
orip
2009-11-08 21:10:29
I like this one. It's concise.
twneale
2010-02-23 17:59:55
+15
A:
Use time.mktime() to convert the time tuple (in localtime) into seconds since the Epoch, then use datetime.fromtimestamp() to get the datetime object.
from time import mktime
from datetime import datetime
dt = datetime.fromtimestamp(mktime(struct))
Rod Hyde
2009-11-08 20:57:16