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

查看:2113
本文介绍了如何将Python 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()转换时间元组(在本地时间)从Epoch以来的秒数,然后使用 datetime。 fromtimestamp()获取datetime对象。

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

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

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