日期,月份,年份的Python时间戳 [英] Python timestamp from day, month, year

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

问题描述

是否可以用Python(日期数)从日期对象中仅创建日期,月份和年份来创建UNIX时间戳(秒数)?我实质上是在寻找午夜的时间戳(小时,分钟和秒为0).

Is it possible to create a UNIX timestamp in Python (number of seconds) with only day, month and year from a date object? I'm essentially looking for what the timestamp would be at midnight (hour, minute and second would be 0).

谢谢!

推荐答案

>>> import time
>>> import datetime
>>> dt = datetime.datetime.strptime('2012-02-09', '%Y-%m-%d')
>>> time.mktime(dt.timetuple())
1328774400.0

-或-

>>> dt = datetime.datetime(year=2012, month=2, day=9)
>>> time.mktime(dt.timetuple())
1328774400.0

对于其他时间类型,例如小时和秒,请转到此处: https://docs.python.org/3/library/datetime.html#strftime-and-strptime-行为

For other Time types like Hour and Second go here: https://docs.python.org/3/library/datetime.html#strftime-and-strptime-behavior

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

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