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

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

问题描述

请帮助我改变datetime对象(例如: 2011-12-17 11:31:00-05:00 )(包括时区)到Unix时间戳(像Python中的函数time.time())

解决方案

另一种方法是:

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

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


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

解决方案

Another way is:

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

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

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

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