python - 将 datetime 转换为 Unix 时间戳并将其转换回 python

我有 dt = datetime(2013,9,1,11),我想获取这个 datetime 对象的 Unix 时间戳。

当我执行 (dt - datetime(1970,1,1)).total_seconds() 时,我得到了时间戳 1378033200

当使用 datetime.fromtimestamp 将其转换回来时,我得到了 datetime.datetime(2013, 9, 1, 6, 0)

时间不匹配。我在这里错过了什么?

最佳答案

解决方案是

import time
import datetime
d = datetime.date(2015,1,5)

unixtime = time.mktime(d.timetuple())

https://stackoverflow.com/questions/19801727/

相关文章:

c - 如何从 C 中通过 PID 在 Linux 中计算进程的 CPU 使用率?

linux - 如何在行号处拆分文件

python - 如何将 NumPy 数组标准化为单位向量?

c - 硬 float 和软 float 有什么区别?

python - Python 'for' 循环中的范围

c - 从C中的文件描述符中检索文件名

python - "inconsistent use of tabs and spaces in i

python - Python 中的 '@=' 符号是什么?

python - Python 中嵌套的 try/except block 是一种好的编程习惯吗?

linux - grep 排除多个字符串