如何使用Python准确计算过去30天(精确到分钟)? [英] How would I compute exactly 30 days into the past with Python (down to the minute)?
本文介绍了如何使用Python准确计算过去30天(精确到分钟)?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
在Python中,我尝试检索的日期/时间恰好是过去30天(30 * 24小时)。目前,我只是在做:
In Python, I'm attempting to retrieve the date/time that is exactly 30 days (30*24hrs) into the past. At present, I'm simply doing:
>>> import datetime
>>> start_date = datetime.date.today() + datetime.timedelta(-30)
返回日期时间的日期对象,但没有时间数据:
Which returns a datetime object, but with no time data:
>>> start_date.year
2009
>>> start_date.hour
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
AttributeError: 'datetime.date' object has no attribute 'hour'
推荐答案
您想使用 datetime
对象,而不只是使用 date
对象:
You want to use a datetime
object instead of just a date
object:
start_date = datetime.datetime.now() - datetime.timedelta(30)
date
仅存储日期和 time
一个时间。 datetime
是带时间的日期。
date
just stores a date and time
just a time. datetime
is a date with a time.
这篇关于如何使用Python准确计算过去30天(精确到分钟)?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文