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.
Convert Date to DateTime Using the datetime Combine Method in Python. In this method, we will first import the date and datetime from the datetime built-in object, then we will extract the current date and minimum time respectively. Both objects will be merged using Python datetime combine built-in method.
strftime. Python strftime() function is present in datetime and time modules to create a string representation based on the specified format string.
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 datetime import datetime from time import mktime dt = datetime.fromtimestamp(mktime(struct))
Like this:
>>> structTime = time.localtime() >>> datetime.datetime(*structTime[:6]) datetime.datetime(2009, 11, 8, 20, 32, 35)
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