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

标签 python datetime timestamp

我有 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())

关于python - 将 datetime 转换为 Unix 时间戳并将其转换回 python,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/19801727/

相关文章:

c# - 在 C# 中将时间跨度转换为日期时间

python - 根据列的字符串值将数字列添加到 pyspark DataFrame

python - 在 python 2 或 python 3 中编写 csv 文件的便携方式

python - 使用协议(protocol) 2 : Python3->2 data 进行 pickle

php - 需要在每个连接的基础上在 PHP/MySQL 中使用 UTC 时间戳转换

php - MySQL 和 PHP 的 ORDER BY 错误

ios - Swift ios 使用 unix 时间戳

python - 获取进程启动的工作目录

c# - 获取特定月份的第一个工作日

objective-c - 12 小时模式的 NSDateFormatter