将Python日期转换为Unix时间戳 [英] Convert Python date to Unix timestamp

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

问题描述

我想将Python date 对象转换为Unix时间戳。

I would like to convert a Python date object to a Unix timestamp.

我已经添加了 time 对象,所以应该是午夜的那个特定日期,但我不知道如何从那里继续。

I already added a time object, so it would be that specific date at midnight, but I don't know how to continue from there.

d = date(2014, 10, 27)
t = time(0, 0, 0)
dt = datetime.combine(d, t)

#How to convert this to Unix timestamp?

我正在使用Python 2.7

I am using Python 2.7

推荐答案

您可以这样获得Unix时间:

You can get the unix time like this:

import time
from datetime import date

d = date(2014, 10, 27)

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

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

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