如何将time.struct_time对象转换为datetime对象? [英] How do you convert a time.struct_time object into a datetime object?

查看:913
本文介绍了如何将time.struct_time对象转换为datetime对象?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如何将Python time.struct_time 对象转换为 datetime.datetime 对象?

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.

推荐答案

使用 time.mktime()转换时间元组(从当地时间开始)到秒的秒数,然后使用 datetime.fromtimestamp( )获取日期时间对象。

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))

这篇关于如何将time.struct_time对象转换为datetime对象?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆