在 Python 中将 datetime 更改为 Unix 时间戳 [英] Change datetime to Unix time stamp in Python

查看:16
本文介绍了在 Python 中将 datetime 更改为 Unix 时间戳的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

请帮我将日期时间对象(例如:2011-12-17 11:31:00-05:00)(包括时区)更改为 Unix 时间戳(如函数 time.time() 在 Python 中).

Please help me to change datetime object (for example: 2011-12-17 11:31:00-05:00) (including timezone) to Unix timestamp (like function time.time() in Python).

推荐答案

另一种方法是:

import calendar
from datetime import datetime
d = datetime.utcnow()
timestamp=calendar.timegm(d.utctimetuple())

Timestamp 是 unix 时间戳,它显示与 datetime 对象 d 相同的日期.

Timestamp is the unix timestamp which shows the same date with datetime object d.

这篇关于在 Python 中将 datetime 更改为 Unix 时间戳的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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